context-guru

module
v0.0.0-...-b624f2c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: Apache-2.0

README

context-guru

Provider-agnostic context engineering for LLM agents: a Go library that shrinks the tokens a request carries — losslessly, or lossy-but-reversible — without touching the agent. Same core runs as an HTTP proxy/gateway or an in-process plugin.

  • Fail open, always — any component error/panic reverts that component only; the original request is always a valid fallback.
  • Never worse — a component that grows the request is reverted.
  • Reversible — every lossy drop leaves a <<cg:HASH>> marker and stashes the original, recoverable via a model-callable context_guru_expand tool or GET /expand.

Architecture

flowchart LR
  A[Agent] -->|chat request| H{Host adapter}
  H -->|proxy: proxy.Handler| P[apply.Body]
  H -->|in-process: AuthBridge plugin| P
  P -->|messages array| PIPE[Pipeline<br/>ordered components]
  PIPE --> P
  P -->|byte-lossless splice| UP[Upstream provider]
  UP -->|response| EX[expand loop]
  EX -->|resolve markers from Store| UP
  EX --> A
  PIPE -.per-component Report.-> M[Emitter / Aggregator]
  PIPE -.stash originals.-> S[(Store<br/>TTL+LRU)]
  EX -.resolve.-> S

Components implement one of two lossiness-typed interfaces and are stacked in config order:

flowchart TD
  C["Component — Name() · Enabled(ctx)"]
  C --> R["Reformat: lossless repack<br/>format · cacheinject"]
  C --> O["Offload: drop + stash, returns cache_keys<br/>skeleton · dedup · collapse · failed_run<br/>cmdfilter · extract · smartcrush · mask · phi_evict"]

Install

Requires Go 1.26 and a C toolchain (CGO_ENABLED=1; the skeleton component uses tree-sitter via cgo). The module pins bifrost with a local replace to ../bifrost/core, so build from the parent directory that holds both repos:

cd .../context-engineering            # dir containing lab-context-engineering/ and bifrost/
CGO_ENABLED=1 go build -o bin/context-guru-proxy \
  ./lab-context-engineering/cmd/context-guru-proxy

Or build the gateway image (see docs/setup.md):

docker build -f lab-context-engineering/Dockerfile -t context-guru:local .

Run the proxy

context-guru-proxy --preset balanced          # or --config cg.yaml

Point any agent at it (one port serves both dialects):

ANTHROPIC_BASE_URL=http://localhost:4000/anthropic
OPENAI_BASE_URL=http://localhost:4000/openai/v1
Flag / env Default Purpose
--preset / PRESET balanced pipeline preset when no --config
--config / CONFIG YAML config (overrides preset)
LISTEN_ADDR :4000 listen address
--openai-upstream / OPENAI_UPSTREAM https://api.openai.com OpenAI upstream base
--anthropic-upstream / ANTHROPIC_UPSTREAM https://api.anthropic.com Anthropic upstream base
OPENAI_API_KEY / ANTHROPIC_API_KEY real key injected on forward (gateway mode); empty = pass client auth through
FORCE_MODEL overwrite the request model (eval-containers EVAL_MODEL)

Routes: POST /openai/v1/chat/completions, POST /anthropic/v1/messages, GET /healthz, GET /stats (savings rollups), GET /expand?id= (recover an offloaded original). Per-request: header x-context-guru-session sets the session key; x-context-guru-bypass: true skips the pipeline.

Integrate

Option What Where
Proxy / gateway context-guru-proxy in front of the provider; the eval-containers gateway image proxy/, cmd/context-guru-proxy/
In-process plugin AuthBridge (Kagenti sidecar) plugin importing this module, running the same pipeline on pctx.Body plugin lives in kagenti-extensions; reuses apply.Body + expand/
(also) bifrost LLMPlugin run the pipeline as a PreRequestHook inside any bifrost deployment adapters/bifrost/

Details in docs/integrations.md.

Docs

  • docs/design.md — architecture: component model, fail-open pipeline, store, session, expand loop, metrics.
  • docs/components.md — every registered component: how it works, before→after, lossiness, config, best use.
  • docs/integrations.md — proxy gateway vs AuthBridge plugin, with request paths.
  • docs/setup.md — setup + a concrete SWE-bench run through the eval-containers gateway.
  • docs/RESULTS.md — per-component SWE-bench benchmark (Claude Code, claude-sonnet-4-6): mask ≈27% token savings, no reward loss.

License

Apache-2.0. See LICENSE.

Directories

Path Synopsis
adapters
bifrost
Package bifrost adapts context-guru's pipeline to bifrost's LLMPlugin interface: our components run as a pre-LLM-call hook (design D2).
Package bifrost adapts context-guru's pipeline to bifrost's LLMPlugin interface: our components run as a pre-LLM-call hook (design D2).
Package apply is the one place the pipeline meets a raw wire request, shared by every host adapter (the bifrost proxy and the AuthBridge plugin).
Package apply is the one place the pipeline meets a raw wire request, shared by every host adapter (the bifrost proxy and the AuthBridge plugin).
cmd
context-guru-proxy command
Command context-guru-proxy is the LLM proxy integration and the eval-containers gateway.
Command context-guru-proxy is the LLM proxy integration and the eval-containers gateway.
Package components defines context-guru's component model: the abstract API every context-engineering operation implements, the per-component report used for metrics, the runtime context handed to each component, and the pipeline that stacks them in configured order.
Package components defines context-guru's component model: the abstract API every context-engineering operation implements, the per-component report used for metrics, the runtime context handed to each component, and the pipeline that stacks them in configured order.
all
Package all blank-imports every built-in component so their init() registrations run.
Package all blank-imports every built-in component so their init() registrations run.
dsl
Package dsl is a declarative, user-extensible text-filter engine, adapted from rtk's TOML filter DSL (design D11).
Package dsl is a declarative, user-extensible text-filter engine, adapted from rtk's TOML filter DSL (design D11).
offload
Package offload holds the lossy-but-reversible components (they drop bytes and stash the original for the expand tool loop).
Package offload holds the lossy-but-reversible components (they drop bytes and stash the original for the expand tool loop).
reformat
Package reformat holds the lossless components (they repack the request denser or add caching hints without losing information).
Package reformat holds the lossless components (they repack the request denser or add caching hints without losing information).
Package config loads context-guru's configuration and builds a pipeline from it.
Package config loads context-guru's configuration and builds a pipeline from it.
Package expand holds the host-agnostic half of reversibility (design D6, after headroom's CCR): the marker format Offload components write, the expand(id) tool definition injected per provider, and resolution of a stashed original from the Store.
Package expand holds the host-agnostic half of reversibility (design D6, after headroom's CCR): the marker format Offload components write, the expand(id) tool definition injected per provider, and resolution of a stashed original from the Store.
internal
buildinfo
Package buildinfo exposes version metadata stamped at build time via -ldflags.
Package buildinfo exposes version metadata stamped at build time via -ldflags.
cheapmodel
Package cheapmodel provides a minimal Anthropic Messages client used as the engine's injected extraction model.
Package cheapmodel provides a minimal Anthropic Messages client used as the engine's injected extraction model.
extract
Package extract is the cheap-model tool-output extractor.
Package extract is the cheap-model tool-output extractor.
tokens
Package tokens estimates token counts using a real BPE tokenizer (o200k_base, the modern GPT family encoding) — an accurate offline proxy.
Package tokens estimates token counts using a real BPE tokenizer (o200k_base, the modern GPT family encoding) — an accurate offline proxy.
treesitter
This file is the pure-Go face of the treesitter package: when the cg_skeleton tag is absent, the real cgo implementation (treesitter.go) is excluded and this empty package takes its place, so `go build ./...` under CGO_ENABLED=0 links no tree-sitter grammars.
This file is the pure-Go face of the treesitter package: when the cg_skeleton tag is absent, the real cgo implementation (treesitter.go) is excluded and this empty package takes its place, so `go build ./...` under CGO_ENABLED=0 links no tree-sitter grammars.
Package metrics turns component/run reports into telemetry.
Package metrics turns component/run reports into telemetry.
Package proxy is the context-guru HTTP proxy: it runs the component pipeline on inbound chat requests, then forwards them to the configured upstream provider.
Package proxy is the context-guru HTTP proxy: it runs the component pipeline on inbound chat requests, then forwards them to the configured upstream provider.
Package schema wraps bifrost's provider-agnostic chat schema with the helpers context-guru components need: token accounting, deep-clone for fail-open snapshots, tool-result iteration, and byte-preservation for lossless round-trips of provider-specific fields.
Package schema wraps bifrost's provider-agnostic chat schema with the helpers context-guru components need: token accounting, deep-clone for fail-open snapshots, tool-result iteration, and byte-preservation for lossless round-trips of provider-specific fields.
Package session resolves the conversation key that state is scoped to.
Package session resolves the conversation key that state is scoped to.
Package store holds context-guru's cross-call state behind one interface so both hosts (bifrost proxy, AuthBridge plugin) share it.
Package store holds context-guru's cross-call state behind one interface so both hosts (bifrost proxy, AuthBridge plugin) share it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL