This CLI tool analyzes AI agent trajectories against policy guards, validating agent interactions against policy constraints and generating detailed analysis reports.
The analyzer processes trajectory files containing AI agent interactions and validates them against policy constraints defined in guard files. It generates CSV reports with detailed analysis results including policy violations, guard evaluations, and execution metrics.
python -m policy_violation.analysis <trajectory_file> [OPTIONS]| Argument | Required | Description |
|---|---|---|
TRAJECTORY_FILE |
Yes | Path to the canonical JSON trajectory file to analyze |
| Option | Short | Required | Description |
|---|---|---|---|
--guards-dir |
-g |
Yes | Path to guards directory containing policy definitions |
--results-dir |
-r |
Yes | Output directory for analysis results |
--wrapper-file |
-w |
Yes | Path to wrapper Python file (e.g., generated/api_wrapper.py) |
--limit |
-l |
No | Limit number of simulations to process (default: 0 = process all) |
--simulation-id |
-s |
No | Process only the simulation with this specific ID |
--verbose |
-v |
No | Enable verbose debug logging |
Analyze a trajectory file with all required options:
python -m policy_violation.analysis \
data/trajectory.json \
--guards-dir generated \
--results-dir results \
--wrapper-file generated/api_wrapper.pyUse short option flags:
python -m policy_violation.analysis \
data/trajectory.json \
-g generated \
-r results \
-w generated/api_wrapper.pyProcess only the first 10 simulations:
python -m policy_violation.analysis \
data/trajectory.json \
--guards-dir generated \
--results-dir results \
--wrapper-file generated/api_wrapper.py \
--limit 10Process only a specific simulation by ID:
python -m policy_violation.analysis \
data/trajectory.json \
--guards-dir generated \
--results-dir results \
--wrapper-file generated/api_wrapper.py \
--simulation-id "2b5333f7-4e7a-4694-9d6e-45bd27909cdf"Enable detailed debug logging:
python -m policy_violation.analysis \
data/trajectory.json \
--guards-dir generated \
--results-dir results \
--wrapper-file generated/api_wrapper.py \
--verboseCombine multiple options:
python -m policy_violation.analysis \
data/trajectory.json \
-g generated \
-r results \
-w generated/api_wrapper.py \
-l 5 \
-vBefore running the analyzer, you must have:
-
Generated Guards: Use the guards CLI to generate policy guards
-
Generated Wrapper: Use the wrappers CLI to generate the wrapper file
-
Trajectory File: A canonical JSON trajectory file containing agent interactions
- Must be in the canonical JSON format
-
LLM Configuration (Required for Dynamic LLM Wrapper): Set environment variables in
.envfileMODEL_NAME: LLM model name (e.g.,gpt-4o,claude-3-5-sonnet-20241022)LLM_PROVIDER: Provider name (e.g.,openai,anthropic,azure)LLM_API_KEY: API key for the LLM providerLLM_API_BASE: API base URL (optional, provider-specific)- See
.env.examplefor configuration examples
The analyzer supports different wrapper implementations for handling API calls during trajectory analysis:
The standard wrapper searches the conversation history for exact matches of tool calls and their results. If a tool call with matching arguments is found in the history, it returns the cached result.
Use case: Fast analysis when all tool calls in the trajectory have exact matches in history.
The DynamicLLMHistoryAPIWrapper uses an LLM to intelligently answer tool calls by analyzing the conversation history. When a tool call is not found in history, it:
- Searches for exact matches in conversation history first
- If no exact match is found, uses an LLM to:
- Analyze previous tool call results
- Extract relevant information from scattered data across multiple tool calls
- Construct partial or complete responses based on available evidence
- Return
nullonly when no relevant information exists in history
Use case: More flexible analysis when tool calls may have variations or when information needs to be synthesized from multiple prior tool calls.
How to use:
- Configure LLM settings in
.envfile (see Prerequisites section) - Pass the path to
DynamicLLMHistoryAPIWrapperas the wrapper file:--wrapper-file src/policy_violation/analysis/dynamic_llm_api_wrapper.py
Example command:
python -m policy_violation.analysis \
data/trajectory.json \
--guards-dir generated \
--results-dir results \
--wrapper-file src/policy_violation/analysis/dynamic_llm_api_wrapper.pyNote: The DynamicLLMHistoryAPIWrapper class automatically receives LLM configuration from environment variables and uses the domain information from the guards directory.
Benefits:
- Handles variations in tool call arguments
- Synthesizes information from multiple tool calls
- More robust to incomplete or partial data in history
- Reduces false positives from missing exact matches
Trade-offs:
- Slower than history-based wrapper (requires LLM calls)
- Requires LLM API access and configuration
- May incur API costs
The tool generates:
- CSV file with detailed analysis results in the specified results directory
- Filename format:
{trajectory_file_name}_results.csv - Contains columns for:
- Simulation metadata
- Guard evaluation results
- Policy violations
- Execution metrics
- Error information
Example output:
results/
└── traj_claude-4-sonnet_results.csv
Ensure you've generated guards first using the guards CLI.
Ensure you've generated the wrapper file using the wrappers CLI.
Verify that your trajectory file is in the canonical JSON format. The file must contain valid simulation data with tool calls and responses.
Check that your trajectory file contains simulation data. Use --verbose to see detailed processing information.
- Guards CLI README - Generate guards before analysis
- Wrappers CLI README - Generate wrapper before analysis
- Main Project README - Complete workflow and overview