Skip to content

Commit cde7381

Browse files
committed
Add Persistence View, multi-turn dataset examples, and project hygiene
HTML report: replace Flow tab with Persistence View (heatmap of content blocks × requests, blue intensity = token cost, COMPACT? annotations). Flow code retained but hidden; Artifacts pane unchanged. Multi-turn examples: add SWE-agent, lmcache, OpenHands trajectory demos with conversion scripts and pre-converted session fixtures. README gains a Findings Across Public Datasets table (3 datasets, real profiler output). Project hygiene: remove toolathon fixtures and GIFs; refresh screenshots to match new views; update CLAUDE.md with accurate architecture, format strategy, and HTML view names; update skill with missing issue codes; remove planned OTel from plugin manifests; add .gitignore entries.
1 parent 68d607b commit cde7381

33 files changed

Lines changed: 3529 additions & 2626 deletions

.claude-plugin/marketplace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"plugins": [
1111
{
1212
"name": "context-profiler",
13-
"description": "Analyze agent traces, transcripts, Langfuse exports, OpenTelemetry spans, and raw provider requests with context-profiler.",
13+
"description": "Analyze agent traces, transcripts, Langfuse exports, and raw provider requests with context-profiler.",
1414
"source": "./",
1515
"strict": false,
1616
"skills": [

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ trace-*.json
4141
.claude/
4242
.cursor/
4343

44+
# Playwright MCP artifacts
45+
.playwright-mcp/
46+
4447
# uv
4548
uv.lock
4649

4750
# Example generated files (keep raw data, ignore generated)
4851
examples/report.html
49-
examples/session.jsonl

.plugin/plugin.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "context-profiler",
33
"description": "Agent context analysis harness for diagnosing context growth, tool bloat, repeated content, and turn-to-turn trace evolution.",
44
"version": "0.1.0",
5+
"repository": "https://github.com/Turdot/context-profiler",
56
"author": {
67
"name": "Yanpeng Wang"
78
},

CLAUDE.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ This file guides AI coding agents working on `context-profiler`.
44

55
## Project Overview
66

7-
`context-profiler` is a trace-source agnostic context analysis harness for LLM agents. It accepts raw provider requests, observability traces, local coding-agent transcripts, and future benchmark trajectories, then reports how context grows, repeats, and concentrates around tools/artifacts.
7+
`context-profiler` is the evidence layer for context engineering. It accepts raw provider requests, observability traces, and agent trajectories, then reports how context grows, repeats, and concentrates around tools/artifacts — so humans and agents know what to compact and where it's safe to cut.
88

9-
Core boundary: this project analyzes traces. It does not fetch traces, replay agent loops, or execute third-party tools.
9+
Core boundary: the library and CLI analyze trace files or stdin. Agents may fetch user-requested trace exports with tools such as `curl` before invoking `context-profiler`, but core should not replay agent loops or execute third-party tools.
1010

1111
## Common Commands
1212

@@ -17,29 +17,47 @@ PYTHONPATH=src uv run --with pytest pytest tests/test_smoke.py -v
1717
# Check CLI entry point
1818
PYTHONPATH=src uv run context-profiler --help
1919

20-
# Analyze a raw request
21-
PYTHONPATH=src uv run context-profiler analyze tests/fixtures/repeated_tool_calls.json --format openai
20+
# Analyze a multi-turn session (canonical demo)
21+
PYTHONPATH=src uv run context-profiler analyze examples/swe_agent/session.jsonl --format openai --html /tmp/report.html
2222

23-
# Diagnose a transcript
24-
PYTHONPATH=src uv run context-profiler diagnose tests/fixtures/cursor_transcript.jsonl --format cursor-jsonl --json
23+
# Diagnose for agent consumption
24+
PYTHONPATH=src uv run context-profiler diagnose examples/swe_agent/session.jsonl --format openai --json
2525

26-
# Generate HTML
27-
PYTHONPATH=src uv run context-profiler analyze tests/fixtures/cursor_transcript.jsonl --format cursor-jsonl --html /tmp/context-profiler-report.html
26+
# Analyze a raw request
27+
PYTHONPATH=src uv run context-profiler analyze tests/fixtures/repeated_tool_calls.json --format openai
2828
```
2929

3030
## Architecture
3131

3232
- `src/context_profiler/cli.py` — Click CLI entry point.
33+
- `src/context_profiler/models.py` — core data model (Session, APIRequest, Message, ContentBlock, BlockType, Role).
34+
- `src/context_profiler/adapters/` — format-specific input adapters (OpenAI, Anthropic, Langfuse, agent-trace, transcript).
3335
- `src/context_profiler/formats.py` — format registry exposed to agents.
3436
- `src/context_profiler/schemas.py` — JSON Schemas for agent-readable contracts.
3537
- `src/context_profiler/validation.py` — validation and canonical normalization helpers.
3638
- `src/context_profiler/profiler.py` — session/request loading and analyzer orchestration.
3739
- `src/context_profiler/analyzers/` — token, repeat, and field analyzers.
3840
- `src/context_profiler/context_diff.py` — turn-to-turn diff evidence and hints.
41+
- `src/context_profiler/session_insights.py` — session-level carryover, budget pressure, artifact lifecycle, and propagation analysis.
3942
- `src/context_profiler/diagnostics.py` — stable issue-code diagnosis JSON.
4043
- `src/context_profiler/reporters/` — CLI, JSON, and HTML reports.
41-
- `src/context_profiler/templates/report.html` — self-contained report UI.
44+
- `src/context_profiler/templates/report.html` — self-contained report UI (views: Icicle, Tools, Persistence).
4245
- `skills/` — public Agent Skills distribution.
46+
- `examples/` — multi-turn demo datasets with conversion scripts.
47+
48+
## HTML Report Views
49+
50+
The interactive HTML report has three main views:
51+
52+
- **Icicle** — token distribution per request, timeline navigation, semantic/diff color modes.
53+
- **Tools** — tool token hotspots, invocation detail, schema size.
54+
- **Persistence** — heatmap of content blocks × requests showing what persists across turns (replaces the old Flow view).
55+
56+
## Format Strategy
57+
58+
Core adapters (tested, maintained): `openai`, `anthropic`, `langfuse`, `cursor-jsonl`, `claude-code-jsonl`, `agent-trace`.
59+
60+
For new benchmark trajectory formats (SWE-agent, T1, tau-bench, etc.), write conversion scripts in `examples/<dataset>/convert.py` that output OpenAI-format JSONL. Do not add new core adapters for every dataset.
4361

4462
## Format and Analyzer Rules
4563

@@ -51,9 +69,9 @@ PYTHONPATH=src uv run context-profiler analyze tests/fixtures/cursor_transcript.
5169

5270
## Product Boundaries
5371

54-
Do not add provider-specific fetch clients to core. Agents can use Langfuse public API exports via `curl`, SDKs, Hugging Face tooling, or local transcript discovery to obtain data, then pass files or stdin to `context-profiler`. Prefer direct Langfuse public API exports over `langfuse-cli` for trace analysis because the CLI can omit fields needed for nested observations and generations.
72+
Do not add provider-specific fetch clients to core. Agents can use Langfuse public API exports via `curl`, SDKs, Hugging Face tooling, or local transcript discovery to obtain data when the user asks, then pass files or stdin to `context-profiler`. Prefer direct Langfuse public API exports over `langfuse-cli` for trace analysis because the CLI can omit fields needed for nested observations and generations.
5573

56-
Do not make Toolathlon a first-class multi-turn demo. Prefer genuinely multi-turn datasets such as `pagarsky/agent-trace`, `cx-cmu/agent_trajectories`, and SWE-agent trajectories for future research examples.
74+
Canonical multi-turn demo datasets are in `examples/`: SWE-agent trajectories (nebius), lmcache-agentic-traces (sammshen), and OpenHands trajectories (nvidia). For new datasets, write conversion scripts in `examples/<dataset>/convert.py` rather than adding core adapters.
5775

5876
## Skill Distribution
5977

README.md

Lines changed: 106 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,75 @@
44
[![Python](https://img.shields.io/pypi/pyversions/context-profiler)](https://pypi.org/project/context-profiler/)
55
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
66

7-
Trace-source agnostic context analysis harness for LLM agents.
7+
The evidence layer for context engineering. Profile before you prune.
88

9-
Bring any agent trace, loop, transcript, or raw provider request. `context-profiler` explains how the context grows, which tools dominate it, what repeats, and where turn-to-turn changes happen.
9+
`context-profiler` turns raw provider requests, observability exports, and agent trajectories into evidence about how context grows, repeats, and concentrates — so you know **what** to compact and **where** it's safe to cut.
1010

11-
![Icicle view — token distribution breakdown](assets/demo-snapshot.gif)
11+
<p align="center">
12+
<img src="assets/readme/hero.png" alt="context-profiler: Icicle view showing token distribution across a 31-turn SWE-agent session" width="100%">
13+
</p>
1214

1315
## Why
1416

15-
Agent systems fail quietly when context grows too large, repeats stale observations, carries verbose tool payloads, or churns on the same artifact for many turns. Observability tools tell you what happened. `context-profiler` focuses on what happened to the context.
17+
Compression tools (LLMLingua, `/compact`, Mem0) execute blindly. They don't tell you what's redundant, what's safe to remove, or what downstream references will break. `context-profiler` fills the missing step:
1618

17-
It is designed for both humans and agents:
18-
19-
- Humans get an interactive HTML report with timeline, icicle, diff, and tool views.
20-
- Agents get stable JSON from `validate`, `diagnose`, and `schema` commands.
21-
- Skills teach Cursor, Claude Code, and other Agent Skills compatible tools when to call the CLI.
22-
23-
![Session mode — timeline and diff](assets/demo-session.gif)
24-
25-
## What It Finds
26-
27-
- **Tool bloat**: tool inputs/results dominate the visible context.
28-
- **Context growth spikes**: individual turns that add large payloads.
29-
- **Repeated content**: exact or near-duplicate blocks retained in context.
30-
- **Repeated tool inputs**: large repeated tool arguments.
31-
- **Artifact churn**: the same file/component/path appears across many tool calls.
32-
- **Partial-scope warnings**: transcripts are useful, but not raw provider requests.
33-
34-
## Research Context
35-
36-
`context-profiler` is motivated by recent work showing that long-horizon agents are constrained not only by model quality, but also by how their working context is retained, compressed, and reused across turns.
19+
```
20+
trace → profile → human review → prune/compact decision
21+
```
3722

38-
Related work:
39-
40-
- **ByteDance Seed — _Scaling Long-Horizon LLM Agent via Context-Folding_**
41-
Studies context management for long-horizon agents through folding and summarizing intermediate sub-trajectories. This motivates `context-profiler`'s focus on turn-to-turn context diffs, retained observations, and compression/pruning evidence.
42-
- **SWE-agent — _Agent-Computer Interfaces Enable Automated Software Engineering_**
43-
Shows the importance of the agent-computer interface for software-engineering agents, motivating analysis of tool calls, terminal output, and artifact churn.
44-
- **WebArena — _A Realistic Web Environment for Building Autonomous Agents_**
45-
Demonstrates the value of realistic multi-step agent trajectories, motivating support for loop/transcript analysis rather than only single prompt snapshots.
23+
Built for both humans and agents:
24+
- **HTML reports** — interactive timeline, icicle, persistence heatmap, tools, diff, findings
25+
- **JSON contracts** — stable issue codes and evidence for automated agent workflows
26+
- **Trace-source agnostic** — same analysis across OpenAI, Anthropic, Langfuse, and public trajectory datasets
27+
28+
## Report Views
29+
30+
<table>
31+
<tr>
32+
<td width="50%">
33+
<img src="assets/readme/semantic.png" alt="Icicle view with semantic color and diff mode" width="100%">
34+
<br>
35+
<strong>Icicle</strong>
36+
<br>
37+
Token distribution per request. Semantic color by role, diff mode for additions/removals.
38+
</td>
39+
<td width="50%">
40+
<img src="assets/readme/persistence.png" alt="Persistence heatmap showing content blocks across requests" width="100%">
41+
<br>
42+
<strong>Persistence</strong>
43+
<br>
44+
Which content blocks survive across turns. Blue = token cost. Red = compact candidate.
45+
</td>
46+
</tr>
47+
<tr>
48+
<td width="50%">
49+
<img src="assets/readme/tools.png" alt="Tools view with token table and invocation detail" width="100%">
50+
<br>
51+
<strong>Tools</strong>
52+
<br>
53+
Which tools dominate the context budget and their invocation details.
54+
</td>
55+
<td width="50%">
56+
<img src="assets/readme/findings.png" alt="Findings drawer with grouped diagnosis issues" width="100%">
57+
<br>
58+
<strong>Findings</strong>
59+
<br>
60+
Issue codes with severity, evidence, and actionable recommendations.
61+
</td>
62+
</tr>
63+
</table>
64+
65+
## Findings Across Public Datasets
66+
67+
Profiled on real multi-turn agent trajectories from public benchmarks:
68+
69+
| Dataset | Domain | Turns | Total Tokens | Redundancy | Top Issue | Carryover |
70+
|---------|--------|-------|-------------|------------|-----------|-----------|
71+
| [SWE-agent](https://huggingface.co/datasets/nebius/SWE-agent-trajectories) | Coding agent | 31 | 27.1K | 26.9% | `REPEATED_CONTENT_BLOCK` | 231K across 20 blocks |
72+
| [lmcache](https://huggingface.co/datasets/sammshen/lmcache-agentic-traces) | KV-cache traces | 35 | 36.5K | 1.4% | `REPEATED_CONTENT_BLOCK` | 403K across 20 blocks |
73+
| [OpenHands](https://huggingface.co/datasets/nvidia/SWE-Zero-openhands-trajectories) | Tool-heavy agent | 34 | 23.9K | 0.2% | `REPEATED_CONTENT_BLOCK` | 383K across 20 blocks |
74+
75+
All examples are included in [`examples/`](examples/) with conversion scripts and pre-converted session files.
4676

4777
## Install
4878

@@ -68,18 +98,17 @@ PYTHONPATH=src uv run context-profiler --version
6898

6999
## Quick Start
70100

71-
Analyze a raw provider request:
101+
Analyze a multi-turn agent session (SWE-agent trajectory included):
72102

73103
```bash
74-
context-profiler analyze request.json --format auto
75-
context-profiler diagnose request.json --format auto --json
104+
context-profiler analyze examples/swe_agent/session.jsonl --format openai --html report.html
76105
```
77106

78-
Analyze a coding-agent transcript:
107+
Analyze a raw provider request:
79108

80109
```bash
81-
context-profiler diagnose cursor-transcript.jsonl --format cursor-jsonl --json
82-
context-profiler analyze claude-code-session.jsonl --format claude-code-jsonl --html report.html
110+
context-profiler analyze request.json --format auto
111+
context-profiler diagnose request.json --format auto --json
83112
```
84113

85114
Analyze a Langfuse export:
@@ -124,6 +153,32 @@ Generate an interactive report:
124153
context-profiler analyze session.jsonl --html report.html
125154
```
126155

156+
## CLI Output
157+
158+
The terminal report gives a quick read on context budget, repeated content, and tool hotspots before you open the HTML report.
159+
160+
```text
161+
╭──────────────────────────────────────────────────────────────────────────────╮
162+
│ context-profiler | mode: snapshot | source: │
163+
│ tests/fixtures/repeated_tool_calls.json │
164+
╰──────────────────────────────────────────────────────────────────────────────╯
165+
166+
⚠ Warnings
167+
• Content duplication: 476 redundant tokens (60.2% of total)
168+
169+
Token Distribution
170+
Category Tokens % of Total
171+
Total Input 791 100%
172+
System Prompt 13 1.6%
173+
Tool Definitions 83 10.5%
174+
Messages (assistant) 609 77.0%
175+
Messages (tool) 70 8.8%
176+
Messages (user) 16 2.0%
177+
178+
Top Tools by Token Usage
179+
generate_canvas_component 595 75.2%
180+
```
181+
127182
## Agent-Friendly CLI Harness
128183

129184
`context-profiler` is strict about supported formats but helpful when input does not match. Agents can discover contracts and adapt unsupported traces without asking users to reshape data manually.
@@ -179,16 +234,6 @@ Use `context-profiler formats list --json` for the current machine-readable regi
179234

180235
For `agent-transcript`, analysis is intentionally marked `partial`: hidden system prompts, rules, tool definitions, MCP schemas, and provider compaction may not be present.
181236

182-
## HTML Report
183-
184-
The HTML report is self-contained and keeps the existing profiler style:
185-
186-
- **Icicle view**: hierarchical token breakdown, zoom, breadcrumb navigation.
187-
- **Context timeline**: growth over turns, with small markers for large additions/tool-input spikes.
188-
- **Diff mode**: unchanged / added / removed content between requests.
189-
- **Tools view**: per-tool token table and invocation details.
190-
- **Detail panel**: selected node or turn-level diff evidence.
191-
192237
## Example Diagnosis
193238

194239
```json
@@ -239,6 +284,19 @@ Recommended demo order:
239284
3. Langfuse trace export.
240285
4. Multi-turn academic trajectories such as `pagarsky/agent-trace`, `cx-cmu/agent_trajectories`, or SWE-agent traces.
241286

287+
## Research Context
288+
289+
`context-profiler` is motivated by recent work showing that long-horizon agents are constrained not only by model quality, but also by how their working context is retained, compressed, and reused across turns.
290+
291+
Related work:
292+
293+
- **ByteDance Seed — _Scaling Long-Horizon LLM Agent via Context-Folding_**
294+
Studies context management for long-horizon agents through folding and summarizing intermediate sub-trajectories. This motivates `context-profiler`'s focus on turn-to-turn context diffs, retained observations, and compression/pruning evidence.
295+
- **SWE-agent — _Agent-Computer Interfaces Enable Automated Software Engineering_**
296+
Shows the importance of the agent-computer interface for software-engineering agents, motivating analysis of tool calls, terminal output, and artifact churn.
297+
- **WebArena — _A Realistic Web Environment for Building Autonomous Agents_**
298+
Demonstrates the value of realistic multi-step agent trajectories, motivating support for loop/transcript analysis rather than only single prompt snapshots.
299+
242300
## Docs
243301

244302
- [CLI harness design](docs/design/cli-harness.md)

assets/demo-session.gif

-8.32 MB
Binary file not shown.

assets/demo-snapshot.gif

-5.94 MB
Binary file not shown.

assets/readme/diff.png

341 KB
Loading

assets/readme/findings.png

589 KB
Loading

assets/readme/hero.png

374 KB
Loading

0 commit comments

Comments
 (0)