Skip to content

Repository files navigation

Context Runtime

License: MITPythonGoNVIDIA Inception

🚀 NVIDIA Inception Program Member — ReDevOps is a member of the NVIDIA Inception Program, supporting startups advancing AI and accelerated computing. Membership provides access to NVIDIA technology, technical resources, and the startup ecosystem. It does not imply product endorsement by NVIDIA.

An efficiency optimizer for a fleet of apps — a database query planner for LLM context. The application says "I need an answer"; the runtime decides what the model sees — what to retrieve, compress, route, and verify — emits an inspectable, replayable plan, and learns from the outcome. It does for AI context what query planners did for SQL. See POSITIONING.md for the thesis.

It optimizes any app with (a) a decision point about what context/config to use and (b) a measurable outcome. Eleven tenants are built and green (each number is the learned-vs-baseline reward its offline example in examples/ prints):

TenantContext Runtime tunesResult
sidekickwhich skills to recall · budgetdrop-in for SkillStore; 67% vs 33% naive baseline acceptance
redevops-ragpool · limit · threshold · rerank … per queryContextRuntimeRetrieverTuner; 0.780 vs 0.428 vs fixed default
edge-sentinel (SOC)which sources to pull per alert (CrowdSec · threat-intel · EDR)tool-using + approval-gated; 0.900 vs 0.800 always-full baseline
growth-enginewhich attribution window + source bundle per lead-source query7.851 vs 5.282 vs fixed window
control-towerwhich Metabase query set per "ask anything" question5.326 vs 1.643 vs core query set
agentic-billingwhich usage/invoice/dunning signals to pull per account4.122 vs 2.442 vs full-stack
social-autopilotwhich channel/timing/content strategy per goal3.875 vs 0.773 vs fixed strategy
agentic-supportwhich KB/tickets/account context to retrieve per ticket3.679 vs 2.394 vs full-context
agentic-bookswhich ledgers/reports to pull per books question3.632 vs 2.430 vs full-books
market-radarwhich competitor watches to sweep per intel question3.611 vs 0.403 vs full-sweep
agentic-compliancewhich rule-family evidence to pull per finding3.562 vs 2.463 vs full-evidence

Reward numbers are single-run outputs of each tenant's examples/ script — re-run to reproduce (they drift with model/data changes). The sidekick and redevops-rag rows are current; the others may need a refresh.

PYTHONPATH=. python examples/sidekick_learning.py # discrete-strategy bandit
PYTHONPATH=. python examples/rag_tuning.py # numeric-knob tuning
PYTHONPATH=. python examples/soc_triage.py # tool-using cybersecurity tenant

Plus the ToolPlugin seam (context_runtime/tools/ — how plans reach external systems, with an approval-gated audit trail) and trace exporters (context_runtime/observability/ exporters.py — JSONL offline, or Langfuse / OpenLLMetry-OTel when the extras are installed).

Status: runnable reference implementation — Python and Go at feature parity. Runs fully offline with stub models; real bindings are wired and lazy-imported: LiteLLM models, and redevops-rag / DuckDB / Postgres retrieval (BM25 · dense · hybrid · graph-PPR · community · sharded routing · cross-modal). Beyond the initial slice it now ships score calibration, a learned quality-aware planner, and EXPLAIN for the retrieval decision (see Also shipped). See SPEC.md §10 for the conformance checklist these tests assert against.

The agentic fleet

The reward table above is the measured slice; the full fleet is 15 native agent services + a control plane, mirrored from the live demo at demo.redevops.io. Each service wraps a mature OSS core and exposes /health, a Material-3 dashboard, and its agent endpoints — the Context Runtime is the decision layer inside each (which context / tools / config per request), while the core does the domain work:

AppOSS coreAppOSS core
agentic-billingLagomarket-radarchangedetection
agentic-booksERPNextedge-sentinel (SOC)CrowdSec
agentic-supportChatwootagentic-complianceOpenSCAP
agentic-crmERPNext (CRM)control-towerMetabase
social-autopilotPostizgrowth-engineUmami
lifecycleListmonkguideredevops-rag

Plus outreach-engine (Twenty CRM), and growth-assistant + agentic-privacy (both on ERPNext — leads / contacts, not books), and the control plane (agentic_os: deploy / observe / approve, /m/<app> proxy + a module catalog). Most have the offline reward examples shown above (examples/<app>.py); the rest are native realizations without a standalone tuner.

The enterprise line (CR-enterprise, v4) adds the commercial open-core layer that composes with this optimizer rather than replacing it: Policy-Constrained Planning — provider / budget / PII / data-residency / mandatory-verification / human-approval predicates define the feasible plan set before the cost model ranks, so every execution can be proven within governance — and Trust-Aware Execution, a trust ledger built from operator acceptance / overrides / regenerations / grounded abstention, so the planner optimizes for the strategy operators will actually rely on. (The mission-runtime line has since shipped — the v6 mission cockpit is live at demo.redevops.io.)

Install

pip install -e .# core (offline stub path, zero heavy deps)
pip install -e ".[litellm]"# real models across 100+ providers
pip install -e ".[rag]"# redevops-rag — single-hop hybrid retrieval
pip install -e ".[hipporag]"# HippoRAG — multi-hop graph retrieval (the planner picks per query)

Single-hop vs multi-hop is a per-query decision. The planner classifies intent and routes: BM25/hybrid (redevops-rag) when the answer is in one chunk, graph (HippoRAG) when it lives in the connections between documents — and the cost model only pays the graph premium when it's warranted. python examples/hop_routing.py shows single-hop missing the bridge document that multi-hop surfaces.

30-second tour

fromcontext_runtimeimportContextRuntime, SourceRefrt=ContextRuntime.default(docs) # offline: stub model + in-memory store# RUN — the core abstraction (plan → build_context → execute → verify)res=rt.run("Explain why deployment X failed",
sources=[SourceRef("docs", "docs")],
constraints={"max_cost_usd": 2.0, "require_citations": True})
print(res.answer, res.cost_usd, res.trace)
# EXPLAIN — debug the plan like SQL (add analyze=True for EXPLAIN ANALYZE)ex=rt.explain("Explain why deployment X failed")
print(ex.intent.bucket, len(ex.candidates), ex.chosen.score.total)
# SIMULATE — forecast cost/latency/tokens with confidence intervals, no executionsim=rt.simulate("Explain why deployment X failed")
print(sim.expected_cost_usd, sim.expected_models, sim.based_on_samples)

Or from the CLI / config:

PYTHONPATH=. python examples/incident_review.py
context-runtime --corpus ./docs run "what's our incident process?"
context-runtime --config context_runtime.yaml explain --analyze "why did deploy X fail?"

Core seams (SPEC) & bindings

The six plugin seams and their initial slice; several "real bindings" below are now shipped (see Also shipped).

Seam (SPEC)initial sliceReal binding
Planner trio (intent/candidate/optimizer)rule-table intent → candidate gen → heuristic cost model(the genuinely new core)
Cost model + statisticsPlanScore weighted utility + pg_statistic-style calibrationlearned/neural (v0.3+)
Optimizerknapsack / greedy-by-utility over the feasible setOR-Tools CP-SAT (v0.2)
Execution Graph IRlinear graph carrying branch/loop/rollback kindsfull shapes (v0.4)
Schedulertopo-sort wavesDagster / cost-aware (v2)
ReasonerSingleShotReasoner (one model)mixtures: plan-worker-critic (v0.3+)
Model pluginoffline StubModelLiteLLM + native cost-tiered routing
Retriever/StoreInMemoryStore (keyword)redevops-rag (DuckDB+BM25+RRF+rerank)
Compressionsidekick clip structural packLLMLingua-2 semantic (v0.1 optional)
Verifiercitation/grounding checkRAGAS / Instructor
Observabilityin-process Trace + JSONOpenLLMetry → Langfuse
Plan Cachenull/always-miss stubsemantic cache (v0.2)

Also shipped

Beyond the initial slice, in both the Python source-of-truth and the Go port (feature parity):

  • Retrieval as a routable capability — BM25 · dense (fastembed ONNX) · hybrid (RRF) · graph (Personalized-PageRank multi-hop) · community · sharded coverage routing for heterogeneous corpora · cross-modal (CLIP image, ColPali multi-vector, video segments — Python) · quantized ANN (TurboVec) · two-stage cost-gated fusion.
  • DSpark-inspired planning — score calibration (isotonic → P(relevant)), grounded abstention, and a load-aware sizer that prunes the expensive stage. Measured v1→v2 lift in BENCHMARKS.md.
  • Quality-aware routing — a quality ledger that tracks learned quality apart from cost per intent, so a genuinely better arm (or provider) wins at equal cost. Opt-in.
  • EXPLAIN (context_runtime/explain.py, POST /librechat/explain, examples/explain.py) — the DB EXPLAIN-ANALYZE analogue for the retrieval decision: every candidate arm ranked with its quality/cost decomposition, the per-method trace with calibrated P(relevant), served/abstain, and reward provenance. Read-only. Visualized at redevops.io/planner.
  • LiteLLM model binding + native cost-tiered routing; DuckDB and Postgres stores.

Benchmarks

Runnable results in BENCHMARKS.md. Headline: the v1 → v2 table measured in both runtimes (Python + Go) — learned-policy precision +14.6 / +11.3 pts, abstention 0 → 100%, expensive-stage depth −62%. Plus retrieval over a heterogeneous financial × medical corpus (coverage routing cuts cross-domain pollution 22→0 with recall intact), the 3-index chat memory tenant (+2.93 learned vs read-all-three), and parallel sharded fusion (5.8× fan-out). Every number is produced by an example in examples/.

Architecture

The decision layer is thin; the substrate is reused. See:

  • ARCHITECTURE.md — the layered design and the cost-based optimizer loop
  • SPEC.md — the normative interface contracts (six plugin seams, IR, trace, plan-cache key)
  • ROADMAP.md — v0.1 → v2 phasing with per-phase exit benchmarks

Test

pip install -e ".[dev]"&& pytest # 360+ tests; test_conformance.py == SPEC §10

About

Context Runtime — a database query planner for LLM context. Decides what a model sees before it answers; plans it, runs it through reused substrate, and learns from the outcome.

Topics

Resources

Stars

12 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages