Token efficiency audit layer for production AI agents
Rust Python SDK v0.2.0 Apache 2.0
A workload-dependent share of agent tokens is structurally redundant — in our own audits of 24 public traces, step redundancy alone runs 36–41%. TraceRazor finds the waste and tells you exactly how to fix it.
TraceRazor reads a completed agent trace (JSON), scores it across thirteen efficiency metrics, and outputs a 0-100 Token Audit Score (TAS) with fix patches. No agent modification required. Everything runs offline.
The pipeline takes a raw trace from any agent framework (LangGraph, CrewAI, OpenAI Agents, or custom), computes all metrics locally, and produces a graded report with actionable fix patches and savings estimates.
| Grade | TAS Range | Meaning |
|---|---|---|
| Excellent | 90-100 | Minimal recoverable waste |
| Good | 70-89 | Minor addressable inefficiency |
| Fair | 50-69 | Significant structural waste |
| Poor | 0-49 | Fundamental issues, restructure needed |
Each metric targets a specific category of token waste. All normalised to 0-1 (higher = better) before being combined into the TAS composite score.
| Code | Metric | Detects | Target |
|---|---|---|---|
| SRR | Step Redundancy Rate | Near-duplicate steps via BoW Jaccard similarity | <15% |
| LDI | Loop Detection Index | Repeated tool-call cycles | <0.10 |
| TCA | Tool Call Accuracy | Failed tool calls and retries | >85% |
| RDA | Reasoning Depth | Over-deep reasoning for task complexity | >0.75 |
| ISR | Information Sufficiency | Steps lacking novel information | >80% |
| TUR | Token Utilisation | Off-task token spending | >0.35 |
| CCE | Context Efficiency | Duplicate context across steps | >0.60 |
| DBO | Decision Optimality | Sub-optimal tool sequences | >0.70 |
| CSD | Semantic Continuity | Reasoning drift mid-trace | ≥0.60 |
| Code | Metric | Detects | Target |
|---|---|---|---|
| VDI | Verbosity Density | Filler words, low-substance content | >0.60 |
| SHL | Sycophancy/Hedging | Excessive politeness and caution | <0.20 |
| CCR | Compression Ratio | Highly compressible text | <0.30 |
| Code | Metric | Detects | Target |
|---|---|---|---|
| GAR | Goal Advancement Ratio | Steps that don't advance toward the goal | ≥0.40 |
When a metric fails, TraceRazor generates a targeted fix patch with estimated token savings. Eight fix types cover all common waste patterns:
After applying fixes and re-running your agent, the Adherence Score checks whether each fix type actually improved its target metric.
| Fix Type | Validates Against |
|---|---|
| tool_schema | TCA (Tool Accuracy) improved |
| context_compression | CCE (Context Efficiency) improved |
| termination_guard | LDI (Loop Detection) improved |
| prompt_insert | RDA (Reasoning Depth) improved |
| verbosity_reduction | VDI (Verbosity Density) improved |
| hedge_reduction | SHL (Sycophancy) improved |
| caveman_prompt_insert | CCR (Compression) improved |
| reformulation_guard | ISR (Information Sufficiency) improved |
13 metrics, scoring engine, fix generation, reports. Zero network dependencies. This is where all analysis happens.
Parsers for raw JSON, LangSmith, and OpenTelemetry trace formats. Converts any format to the internal Trace struct.
Bag-of-words similarity backend. Optional LLM embeddings (OpenAI, Anthropic, or compatible) for enhanced mode.
SurrealDB persistence: traces, known-good-paths, baselines, and anomaly detection rolling windows.
Axum REST API + WebSocket live events + embedded React dashboard. Serves the full UI at port 8080.
Four-layer guardrail interceptor: semantic preservation, scope whitelist, budget injection, verbosity directive.
CLI entry point: audit, optimize, bench, simulate, compare, cost, export.
SDK (pip install tracerazor) + adapters for LangGraph, CrewAI, and OpenAI Agents. All on PyPI v0.2.0.
| Command | Purpose |
|---|---|
tracerazor audit | Score a trace; optionally gate on --threshold |
tracerazor optimize | Rewrite system prompt via LLM to eliminate detected waste |
tracerazor bench | Compare before/after traces, verify savings |
tracerazor simulate | Project TAS impact of removing/merging steps |
tracerazor compare | Side-by-side metric delta between two traces |
tracerazor cost | Monthly savings estimate across trace set |
tracerazor export | Forward trace to OTEL or webhook |
All published to PyPI v0.2.0. Each wraps the core analysis engine with framework-native callbacks.
pip install tracerazor
Framework-agnostic. Use Tracer context manager with any Python agent.
pip install tracerazor-langgraph
Native BaseCallbackHandler. Auto-captures LLM and tool events.
pip install tracerazor-crewai
CrewAI callback adapter. Traces tasks, agent actions, and tool calls.
pip install tracerazor-openai-agents
OpenAI RunHooks adapter. Captures agent ends, tool use, handoffs.
Each agent in a workflow gets its own tracer and independent report. Aggregate metrics across all agents for workflow-level visibility.
5 synthetic traces, each isolating a waste pattern. Run with python benchmarks/run_benchmarks.py.
| Trace | TAS | Grade | Tokens | Waste | Savings | Fixes |
|---|---|---|---|---|---|---|
| bloated-agent | 87.9 | Good | 2,320 | 30% | 693 | 2 |
| clean-agent | 89.4 | Good | 860 | 34% | 289 | 1 |
| looping-agent | 69.4 | Fair | 1,710 | 35% | 603 | 3 |
| reformulator-agent | 86.9 | Good | 1,340 | 32% | 433 | 1 |
| verbose-agent | 77.1 | Good | 2,760 | 53% | 1,455 | 2 |
| Crate | Tests | Covers |
|---|---|---|
| tracerazor-core | 125 | All 13 metrics, scoring, fixes, IAR, simulation, reports |
| tracerazor-ingest | 3 | Raw JSON, LangSmith, OTEL parsers |
| tracerazor-semantic | 5 | BoW similarity, tokenization |
| tracerazor-store | 9 | CRUD, baselines, anomaly detection |
| tracerazor-server | 13 | REST API endpoints, WebSocket |
| tracerazor-proxy | 12 | All 4 proxy layers |
git clone https://github.com/ZulfaqarHafez/tracerazor
cd tracerazor
docker compose up --build
# Dashboard at http://localhost:8080
cargo build --release
./target/release/tracerazor audit trace.json
pip install tracerazor
from tracerazor_sdk import Tracer
with Tracer(agent_name="my-agent") as t:
t.reasoning("Analyzing request...", tokens=200)
t.tool("search", params={}, output="result", success=True, tokens=150)
report = t.analyse()
print(report.summary())
# TAS 78.5/100 [GOOD] | 4 steps, 350 tokens | Saved 90 tokens (26%)
tracerazor audit trace.json --threshold 75
# Exit code 1 if TAS < 75
TraceRazor v0.2.0 | Apache 2.0 | Author: Zulfaqar Hafez
github.com/ZulfaqarHafez/tracerazor