Skip to main content

Dynamic context management for tool-using AI agents

Project description

contextweaver

Dynamic context management for tool-using AI agents.

contextweaver solves the context window problem: as tool catalogs grow and conversations accumulate history, naive concatenation blows past token limits. contextweaver provides phase-specific budgeted context compilation, a context firewall for large tool outputs, result envelopes with structured fact extraction, and bounded-choice routing over large tool catalogs via DAG + beam search.

Features

  • Context Engine — seven-stage pipeline that compiles a phase-aware, budget-constrained prompt from the event log.
  • Context Firewall — intercepts large tool outputs, stores raw data out-of-band, and injects compact summaries.
  • Routing Engine — navigates catalogs of 100+ tools via a bounded DAG so the LLM only sees a focused shortlist.
  • Protocol Adapters — first-class adapters for MCP and A2A protocols.
  • Zero Dependencies — pure Python ≥ 3.10, stdlib only.
  • Deterministic — identical inputs always produce identical outputs.

Installation

pip install contextweaver

Or install from source:

git clone https://github.com/dgenio/contextweaver.git
cd contextweaver
pip install -e ".[dev]"

Quick start

from contextweaver.context.manager import ContextManager
from contextweaver.types import ContextItem, ItemKind, Phase

mgr = ContextManager()
mgr.ingest(ContextItem(id="u1", kind=ItemKind.user_turn, text="How many users?"))
mgr.ingest(ContextItem(id="tc1", kind=ItemKind.tool_call, text="db_query('SELECT COUNT(*) FROM users')", parent_id="u1"))
mgr.ingest(ContextItem(id="tr1", kind=ItemKind.tool_result, text="count: 1042", parent_id="tc1"))

pack = mgr.build_sync(phase=Phase.answer, query="user count")
print(pack.prompt)       # budget-aware compiled context
print(pack.stats)        # what was kept, dropped, deduplicated

Routing large tool catalogs

from contextweaver.routing.catalog import Catalog, load_catalog_json
from contextweaver.routing.tree import TreeBuilder
from contextweaver.routing.router import Router

items = load_catalog_json("catalog.json")
catalog = Catalog()
for item in items:
    catalog.register(item)

graph = TreeBuilder(max_children=10).build(catalog.all())
router = Router(graph, items=catalog.all(), beam_width=3, top_k=5)
result = router.route("send a reminder email about unpaid invoices")
print(result.candidate_ids)

CLI

contextweaver ships with a CLI for quick experimentation:

contextweaver demo                          # end-to-end demonstration
contextweaver init                          # scaffold config + sample catalog
contextweaver build --catalog c.json --out g.json  # build routing graph
contextweaver route --graph g.json --query "send email"
contextweaver print-tree --graph g.json
contextweaver ingest --events session.jsonl --out session.json
contextweaver replay --session session.json --phase answer

Examples

Script Description
minimal_loop.py Basic event ingestion → context build
tool_wrapping.py Context firewall in action
routing_demo.py Build catalog → route queries → choice cards
before_after.py Side-by-side token comparison: WITHOUT vs WITH contextweaver
mcp_adapter_demo.py MCP adapter: tool conversion, session loading, firewall
a2a_adapter_demo.py A2A adapter: agent cards, multi-agent sessions

Run all examples:

make example

Documentation

  • Architecture — package layout, pipeline stages, design principles
  • Concepts — ContextItem, phases, firewall, ChoiceGraph, etc.
  • MCP Integration — adapter functions, JSONL format, end-to-end example
  • A2A Integration — adapter functions, multi-agent sessions

Development

make fmt      # format (ruff)
make lint     # lint (ruff)
make type     # type-check (mypy)
make test     # run tests (pytest)
make example  # run all examples
make demo     # run the built-in demo
make ci       # all of the above

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contextweaver-0.1.4.tar.gz (110.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contextweaver-0.1.4-py3-none-any.whl (93.4 kB view details)

Uploaded Python 3

File details

Details for the file contextweaver-0.1.4.tar.gz.

File metadata

  • Download URL: contextweaver-0.1.4.tar.gz
  • Upload date:
  • Size: 110.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for contextweaver-0.1.4.tar.gz
Algorithm Hash digest
SHA256 aea8862fdb36dc53b64bf096eb641a556fb35ac74dc3120ff9d6bc982bd671be
MD5 460366b21362ab129f144fab798dede1
BLAKE2b-256 1fe2491614bfc3ada9feff467593930ab7618d19890c0c83e072c4c0089dc80f

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextweaver-0.1.4.tar.gz:

Publisher: publish.yml on dgenio/contextweaver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file contextweaver-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: contextweaver-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 93.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for contextweaver-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 420fd206a5b1282af384dde7357f9723efc75658d60d3b8eb16024b75b07e0e4
MD5 46e4b0686a86511e7fa3a7181046a6c4
BLAKE2b-256 5cbe579b60e1a985d2e2b183d12ab14315af40a6d7808f503249522b3b2ef75e

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextweaver-0.1.4-py3-none-any.whl:

Publisher: publish.yml on dgenio/contextweaver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page