AgentLens
Developed at MATS Exploration Phase under Neel Nanda, for a research project with Greg Kocher.
A harness for running multi-session agent trajectories across multiple engines (Claude Code and OpenAI Codex), capturing them in ATIF (Agent Trajectory Interchange Format), and tracking file state changes across sessions.
Built for AI alignment and interpretability research — studying how LLM agents behave across multi-turn, multi-session, multi-agent interactions.
Note: AgentLens supports two engines — Claude Code (via the Claude Agent SDK) and Codex (via the Codex CLI) — selected with the engine config field. Every run is clearly labeled with its engine in the CLI, run_meta.json, and the web UI. Support for additional agents and frameworks is planned — see Roadmap. Some features (especially turn-level replay) are experimental. We welcome PRs and contributions — open an issue if you run into bugs.

What it does
The harness takes a YAML config describing a sequence of sessions (prompts to an agent), runs each session against a working directory via the selected engine (Claude Code or Codex), and produces structured outputs:
- ATIF trajectories — standardized JSON capturing every agent step, tool call, observation, and thinking block
- Shadow git change tracking — automatic tracking of all file changes via an invisible git repo, with per-step write attribution and full unified diffs
- Session chaining — three modes for controlling how sessions relate to each other (isolated, chained, forked)
- Resampling & replay — study behavioral variance at multiple levels: stateless API resampling, intervention testing (edit assistant text, tool results, or system prompts and resample), session-level resampling, and turn-level replay with full tool execution from any branch point
- Subagent capture — separate ATIF trajectories for each subagent invocation, linked to the parent via
SubagentTrajectoryRef
- Auto-judge — an LLM judge evaluates the running trajectory against a rubric every N turns, flags matches, and can early-exit the agent loop; backend-configurable (Anthropic/OpenAI/OpenRouter/custom) and works for both engines
Install
Requires Python >= 3.12 and uv.
git clone <this-repo>
cd agentlens
uv sync
Quick start
If you have a Claude Code subscription (Pro/Max), no API key is needed — the SDK uses your subscription credentials automatically. Otherwise, set an API key:
export ANTHROPIC_API_KEY=sk-ant-... # Anthropic API key
# or
export OPENROUTER_API_KEY=sk-or-... # OpenRouter (set provider: openrouter in config)
Run the smoke test:
harness run tests/smoke.yaml
Inspect results:
harness inspect runs/<run-name>
Browse in the web UI:
cd ui && npm install && npm run dev
# Open http://localhost:5173
Engines
The engine field selects the coding-agent runtime. Both engines share the same trajectory model, shadow-git change tracking, diffs, session modes, capture, resample, and replay — runs are labeled with their engine everywhere (CLI, run_meta.json, ATIF extra.engine, run-dir slug, web UI badge).
| Engine |
Config value |
Runtime |
Auth |
Notes |
| Claude Code |
claude_code (default) |
Claude Agent SDK |
ANTHROPIC_API_KEY or Claude Pro/Max subscription |
Subagents via the agents: config block. Routes via the Anthropic Messages API. |
| Codex |
codex |
Codex CLI codex exec --json (>= 0.135) |
codex login (subscription) or OPENAI_API_KEY; OPENROUTER_API_KEY for provider: openrouter |
Subagents via Codex multi-agent (codex_multi_agent: true). Routes via the OpenAI Responses API, or OpenRouter with provider: openrouter. |
Subagents are captured for both engines as separate, linked ATIF trajectories (a SubagentTrajectoryRef on the spawning step). They use different mechanisms: Claude Code via the agents: config block (Claude-only); Codex via its native multi-agent system — set codex_multi_agent: true to let Codex spawn agents (TOML agent definitions live in ~/.codex/agents/), and AgentLens rebuilds each spawned thread's rollout into a linked subagent trajectory.
# Codex engine
engine: codex
model: "gpt-5.4"
sandbox_mode: workspace-write # read-only | workspace-write | danger-full-access
# Codex via OpenRouter — point Codex at any OpenRouter model:
# engine: codex
# provider: openrouter
# model: "openai/gpt-5.3-codex" # exact OpenRouter slug (vendor prefix required)
Codex via OpenRouter. Set provider: openrouter to route the Codex engine through OpenRouter, then export OPENROUTER_API_KEY. AgentLens injects the required Codex model_providers block automatically (base_url=https://openrouter.ai/api/v1, wire_api=responses). The model must be a full OpenRouter slug including the vendor prefix (e.g. openai/gpt-5.3-codex) — a bare slug is rejected at config load. For Codex, provider is either openai (default) or openrouter.
Codex auth & capture. Normal runs and turn-level replay use whatever codex login configured. API capture (capture_api_requests: true) and the resampling it enables additionally require an API key with active billing — OPENAI_API_KEY for provider: openai or OPENROUTER_API_KEY for provider: openrouter — because capture routes Codex through a proxy via a custom model provider that uses API-key auth (the built-in providers' base URLs can't be overridden). For trajectories + replay only, subscription auth is enough on the OpenAI path; keep capture_api_requests: false. See examples/codex.yaml.
Providers
For the claude_code engine, the provider field routes API calls. The Claude Agent SDK speaks the Anthropic Messages API protocol and only runs Claude models. (For the codex engine, provider selects the Codex model provider — openai (default) or openrouter; see Engines.)
| Provider |
Config value |
Env var |
Notes |
| Anthropic |
anthropic (default) |
ANTHROPIC_API_KEY |
Direct Anthropic API. If no key is set, falls back to Claude Code subscription credentials. |
| OpenRouter |
openrouter |
OPENROUTER_API_KEY |
Routes through OpenRouter. The harness sets ANTHROPIC_BASE_URL automatically. |
| AWS Bedrock |
bedrock |
Standard AWS credentials (AWS_ACCESS_KEY_ID, etc.) |
Sets CLAUDE_CODE_USE_BEDROCK=1. |
| GCP Vertex AI |
vertex |
Standard GCP credentials (GOOGLE_APPLICATION_CREDENTIALS, etc.) |
Sets CLAUDE_CODE_USE_VERTEX=1. |
You can also set base_url in your config to point at a custom Anthropic-compatible endpoint.
With provider: anthropic (the default), if no ANTHROPIC_API_KEY is set, the SDK falls back to your Claude Code subscription credentials from ~/.claude/credentials.json (requires Claude Pro/Max). Usage is covered by your subscription with rate limits rather than per-token billing. If ANTHROPIC_API_KEY is set in your environment, it takes precedence over subscription credentials.
Cost reporting caveat: Cost figures in run_meta.json and the web UI come from the SDK and are based on Anthropic's list pricing regardless of provider. They may not match your actual bill (especially on OpenRouter, Bedrock, or Vertex) and are purely informational when using a Claude Code subscription.
Example configs:
# Anthropic (default) — uses API key or Claude Code subscription
model: "claude-sonnet-4-20250514"
provider: anthropic
# OpenRouter
model: "claude-sonnet-4-20250514"
provider: openrouter
Configuration
Experiments are defined as YAML config files. Here's a full example:
model: "claude-sonnet-4-20250514"
provider: anthropic # anthropic | openrouter | bedrock | vertex
hypothesis: "The agent preserves hedging across sessions" # what this experiment tests
work_dir: "./repos/my_project" # working directory the agent operates in
session_mode: chained # isolated | chained | forked
tags: ["experiment-1"]
system_prompt: |
You are exploring a Python codebase. Use MEMORY.md to keep notes.
allowed_tools: # Claude Code tools the agent can use
- Read
- Grep
- Glob
- Bash
- Write
- Edit
max_turns: 30 # max agent turns per session
permission_mode: bypassPermissions # acceptEdits | bypassPermissions
max_budget_usd: 1.00 # optional spend cap per session
load_project_settings: false # whether to load the repo's CLAUDE.md
memory_file: "MEMORY.md" # auto-seeded file in working dir (default: MEMORY.md)
memory_seed: "# Project Notes\n" # initial content if file doesn't exist
revert_work_dir: true # reset working dir after run (default: false)
sessions:
- session_index: 1
prompt: "Explore the project structure. Take notes in MEMORY.md."
- session_index: 2
prompt: "Read the main module in detail. Update your notes."
- session_index: 3
prompt: "Summarize what you know about this project."
max_turns: 10 # per-session override
Shadow git (change tracking)
All file changes in the working directory are tracked automatically via a shadow git — a bare git repo stored in the run output directory (.shadow_git/). The agent never sees this repo; it uses GIT_DIR/GIT_WORK_TREE env vars to stay invisible.
This enables:
- Full diffs — every file change is captured automatically, no need to declare files upfront
- Turn-level replay — git worktrees provide isolated filesystem copies at any turn's state for parallel replay execution
- Per-step attribution — file writes are detected after each tool-using step and logged to
state_changelog.jsonl
- Session diffs — unified patches showing what each session changed, saved as
session_diff.patch
The working directory does not need to be a git repo. The shadow git works with any directory.
Automatic behaviors
- Memory file is auto-seeded. The harness creates
MEMORY.md (or whatever memory_file is set to) with the memory_seed content if it doesn't already exist.
- Working directory path is injected into the system prompt. The harness appends the absolute path and memory file location to the system prompt so the agent knows where to read/write.
- The agent's cwd is the working directory. Set to the resolved
work_dir.
Session modes
| Mode |
Behavior |
Shadow git action |
isolated |
Each session starts with a fresh conversation. File changes persist. |
No reset |
chained |
Each session resumes from the previous session's conversation. Full context preserved. |
Changes accumulate (no reset) |
forked |
Sessions 2+ fork from session 1. Each sees session 1's context but not each other's. |
Reset to session 1's end state |
Flexible forking with fork_from
For more control than session_mode: forked provides, use fork_from on individual sessions to fork from any prior session — not just session 1:
session_mode: isolated # fork_from overrides session_mode per-session
sessions:
- session_index: 1
prompt: "Explore the codebase and take notes in MEMORY.md"
- session_index: 2
prompt: "Write a security analysis based on your notes"
fork_from: 1 # forks from session 1's conversation
- session_index: 3
prompt: "Write a performance analysis based on your notes"
fork_from: 1 # also forks from session 1 (independent of session 2)
fork_from must reference a session with a lower index. It works with any session_mode — when set, it overrides the mode for that session.
Session resampling with count
To study behavioral variance, run the same forked session multiple times:
sessions:
- session_index: 1
prompt: "Explore the codebase and take notes"
- session_index: 2
prompt: "Write a security analysis based on your notes"
fork_from: 1
count: 5 # run 5 replicates of this session
Replicates use a _rNN