Built in the Open
TraceGC is maintained on GitHub by the developers below.
TraceGC prunes obsolete updates, aborted pipelines, and duplicate tool logs from your agent's execution history — deterministically, locally, with zero extra LLM calls. Original steps remain recoverable.
The core library requires no external third-party imports. Drop it cleanly into any stateful Python workflow layout.
Compaction executes locally without stochastic loops. The same input trace always compiles to the exact same prompt prefix.
Obsolete events scale down into receipt stubs inline. Retain original parameters inside a lightweight local data store.
As LLM agents loop to solve complex tasks, tool outputs, aborted logs, and temporary state mutations accumulate. Prompts hit context limits, prompts trigger high latency, and LLM confusion increases.
TraceGC executes 5 deterministic stages locally, transforming execution graphs into clean, compacted prompt output.
# raw events e001 [decision] "Try search" e002 [tool_call] "google_search" e003 [abandon] ref_to=["e002"]
# pruned output [RECEIPT e002] # Swept dead branch
TraceGC bypasses lossy summaries. Every pruned byte remains retrievable.
Pruned execution logs scale down inline into lightweight token placeholders ([RECEIPT ID]). Hover over the stubs below to simulate recovery calls:
Unlike LLM-based recaps that cost tokens and add latency, compaction logic executes fully locally. Free up LLM workloads and save prompt budgets.
Initialize the TraceGC client wrapper to append logs step-by-step during live agent execution loops, or compile list payloads in batch calls.
Connect context compaction easily. Import optional OpenAI and Anthropic adapters (call_openai_with_compaction) that load dependencies on-demand.
Robust validation for 18 structured event schemas, from state updates to file edits, test runs, and git operations.
Drop context compaction directly into python loops. Code is 100% Python.
from tracegc import TraceGC
# 1. Initialize the client
client = TraceGC()
# 2. Append events incrementally as they occur
client.add_event({
"id": "e001",
"type": "decision",
"timestamp": 1000,
"parent_id": None,
"content": "Start config"
})
client.add_event({
"id": "e002",
"type": "set_var",
"timestamp": 1010,
"parent_id": "e001",
"key": "x",
"value": 10
})
client.add_event({
"id": "e003",
"type": "set_var",
"timestamp": 1020,
"parent_id": "e002",
"key": "x",
"value": 20 # Supersedes x=10
})
# 3. Compact the context history on-demand
result = client.compact()
print(result["prompt"])
# Output: [RECEIPT e002]
x = 20
decision: Start configpip install tracegcpip install tracegc tracegc-storageThe core tracegc library ships with zero external dependencies. Optional official adapters in the tracegc/tracegc monorepo add persistence, agent-framework hooks, and MCP support.
| Package / Module | Version | Python | Target / Dependencies | Primary Entry Point | Install |
|---|---|---|---|---|---|
| tracegc | 0.6.2 | >=3.10 | Zero external dependencies (Core + CLI) | TraceGC(), compact_events() | pip install tracegc |
| tracegc-storage | 0.5.0 | >=3.9 | tracegc (Memory & SQLite persistence) | SQLiteStore("sessions.db") | pip install tracegc-storage |
| tracegc-langgraph | 0.5.0 | >=3.9 | tracegc, langgraph, langchain-core | wrap_graph(graph) | pip install tracegc-langgraph |
| tracegc-crewai | 0.1.0 | >=3.9 | tracegc, crewai | create_step_callback() | pip install tracegc-crewai |
| tracegc-mcp | 0.5.0 | >=3.9 | tracegc, tracegc-storage, mcp>=1.0.0 | tracegc-mcp --db-path ./sessions.db | pip install tracegc-mcp |
| tracegc.middleware | 0.6.2 | >=3.10 | Lazy-loaded optional openai / anthropic | call_openai_with_compaction() | Included in tracegc |
Hover over each element below to inspect the stages of compilation.
Comparative evaluation across Short, Medium, and Long traces measuring token count, compaction latency, API cost, and four deterministic information-survival probes.
| Trace Size | Compaction Method | Avg Tokens | Latency (s) | Cost ($) | Recall | Artifact | Continuation | Decision | Deterministic |
|---|---|---|---|---|---|---|---|---|---|
| Short | full_history | 121.0 | 0.0000 | $0.000000 | 100.0% | 100.0% | 100.0% | 100.0% | n/a |
| Short | truncate_by_event_count | 116.3 | 0.0000 | $0.000000 | 100.0% | 100.0% | 100.0% | 100.0% | n/a |
| Short | truncate_by_token_count | 121.0 | 0.0000 | $0.000000 | 100.0% | 100.0% | 100.0% | 100.0% | n/a |
| Short | ai_summarize_single (flash) | 90.7 | 5.2322 | $0.000036 | 100.0% | 33.3% | 55.6% | 0.0% | No |
| Short | tracegc_pipeline | 75.3 | 0.0011 | $0.000000 | 100.0% | 100.0% | 100.0% | 100.0% | Yes |
| Medium | full_history | 379.7 | 0.0000 | $0.000000 | 100.0% | 100.0% | 100.0% | 100.0% | n/a |
| Medium | truncate_by_event_count | 133.3 | 0.0000 | $0.000000 | 0.0% | 100.0% | 0.0% | 0.0% | n/a |
| Medium | truncate_by_token_count | 212.0 | 0.0001 | $0.000000 | 0.0% | 100.0% | 0.0% | 0.0% | n/a |
| Medium | ai_summarize_single (flash) | 146.7 | 6.3571 | $0.000072 | 66.7% | 88.9% | 100.0% | 0.0% | No |
| Medium | ai_summarize_recursive (flash) | 131.0 | 21.1568 | $0.000082 | 0.0% | 66.7% | 100.0% | 0.0% | No |
| Medium | tracegc_pipeline | 299.0 | 0.0017 | $0.000000 | 100.0% | 100.0% | 100.0% | 100.0% | Yes |
| Long | full_history | 1301.0 | 0.0001 | $0.000000 | 100.0% | 100.0% | 100.0% | 100.0% | n/a |
| Long | truncate_by_event_count | 104.3 | 0.0000 | $0.000000 | 0.0% | 0.0% | 0.0% | 0.0% | n/a |
| Long | truncate_by_token_count | 210.7 | 0.0001 | $0.000000 | 0.0% | 0.0% | 0.0% | 0.0% | n/a |
| Long | ai_summarize_single (flash) | 243.4 | 8.2741 | $0.000171 | 100.0% | 0.0% | 100.0% | 0.0% | No |
| Long | ai_summarize_recursive (flash) | 219.2 | 42.6211* | $0.000199 | 100.0% | 0.0% | 100.0% | 0.0% | No |
| Long | tracegc_pipeline | 1028.3 | 0.0028 | $0.000000 | 100.0% | 100.0% | 100.0% | 100.0% | Yes |
“TraceGC’s token reduction (~21% on Long traces vs.full_history) is more conservative than truncation or AI summarization. The tradeoff is deliberate — nothing is ever discarded, and every pruned event is recoverable viaget_receipt(). It is the only method in this benchmark that scored 100% on all four probes at every trace length while running in ~1.1–2.8 ms locally.”
1. Exact substring matching bias: The decision probe checks for exact substring survival against the original event text. This structurally favors verbatim-preserving methods (tracegc_pipeline, truncation) over paraphrasing (ai_summarize_single/recursive) — a semantically accurate paraphrase can score 0% on this probe. We report probe scores as-is because they are deterministic and reproducible; for downstream LLM answer quality, see Scenario 5 in WRITEUP.md.
2. Cycle collapse verification: Defensive cycle-collapsing behavior is verified separately under synthetic cyclic traces in tests/test_topo_sampler.py. All comparative benchmark numbers above are scored against natural, un-injected event traces.
3. Model & quota constraints: AI summarization was tested on gemini-3.6-flash (Pro tier unavailable at 0 req/day quota). *42.6211s on Long ai_summarize_recursive excludes a 75s API rate-limit retry wait (50.9544s including retry wait).
4. Sample size & unresolved anomaly: Evaluated across 3 runs per trace fixture. ai_summarize_recursive scored 0.0% recall on Medium traces yet 100.0% on Long traces — reported as-is as an open anomaly.
During multi-step reasoning, agents accumulate deep tree loops and trial runs. TraceGC sweeps aborted paths and redundant thoughts automatically, preserving the final clean sequence.
Accumulated loops: 4,200 tokens
Post-compaction: 2,436 tokens
Correctness: 100% decision probe recall
Detailed, honest comparisons with alternate agentic memory architectures.
| Project Paradigm | Focus | Strength | Limitation vs. TraceGC |
|---|---|---|---|
| MemGPT / Letta | OS-style virtual context memory paging to disk | Broad framework tooling, mature agent systems | Heavier architecture. Not a zero-dependency local utility library. |
| Vector DB / RAG Memory | Semantic text search query over raw memories | Excellent semantic recall accuracy at scale | Requires hosted databases. Non-deterministic and lack receipts. |
| AI Summarization | LLM recaps that summarize linear logs periodically | Flexible summaries for natural language prose | Stochastic, adds API cost/latency, zero decision-probe score. |
| Knowledge Graph systems | Structuring experience as entity-relation networks | Extremely rich semantic database indexing | Typically requires LLM parsing. Heavy runtime compared to local events. |
No complex setup scripts. Just pip install and import client modules inside Python.
Downloading tracegc-0.6.2-py3-none-any.whl Installing collected packages: tracegc Successfully installed tracegc-0.6.2
Get instant answers regarding schemas, validation rules, and configuration parameters.
TraceGC is a framework-agnostic, installable library combining deterministic graph-based pruning with recoverable receipts. While existing tools (such as Self-GC, ClawVM, Cognee, ContextNest, Headroom, and MemGPT/Letta) split these approaches across research papers, hosted SaaS products, client-side compressors, or LLM-based summarization routines, TraceGC ships as a simple, drop-in, zero-dependency Python library designed for developers building stateful agent workflows.
By modeling the agent's interaction history (execution traces) as a directed multigraph, TraceGC identifies and removes obsolete or superseded steps, dead execution branches, and cycles. When elements are pruned, TraceGC leaves behind lightweight, deterministic receipt stubs inline, allowing agents to preserve awareness of their history. Furthermore, the complete original content of any pruned step remains fully recoverable on-demand.
TraceGC is maintained on GitHub by the developers below.
Compact event timelines deterministically. Save LLM window space and costs.
Read codebase sources, check test coverages, and download benchmarks.