Context Graph Protocol

The Context Graph Protocol: Advantages and Uniqueness

A research analysis of the seven properties that distinguish Context Graph Protocol as a trust architecture for context retrieval.

Research note. This page is a standalone analysis of why the Open Context Protocol (Context Graph Protocol) represents a qualitatively different approach to context retrieval for AI coding agents. It is written for engineers and researchers evaluating retrieval architectures, not for a quick start. For the implementation guide, see Implementing a provider; for the wire types, see Protocol surface; for conformance, see Running conformance.


Abstract

Every production AI coding agent retrieves context: code snippets, symbol definitions, documentation, prior episodes, graph relationships. The overwhelming industry practice is to treat retrieval as an opaque blob-pipe — a vector search or a grep result stuffed into a prompt with no accountability for cost, provenance, consent, or citation. The Context Graph Protocol (Context Graph Protocol), implemented in this repository as contextgraph-types, contextgraph-host, and contextgraph-conformance, takes a fundamentally different position: context is a first-class, typed, budgeted, provenance-carrying, consent-gated, and conformance-verified unit of exchange. Every frame that enters a prompt is traceable to its source, honest about its cost, gated by recorded consent, and machine-checked for contract compliance — context that an agent, a host, or an auditor can trust as evidence rather than accept on faith.

This document articulates the seven advantages that distinguish Context Graph Protocol from prior approaches, maps each to its enforcement mechanism in the implementation, and explains why the combination is irreducible: removing any single property collapses the trust model back to the blob-pipe.


Table of contents

  1. The problem: context as an unaccountable blob-pipe
  2. The seven advantages
  3. Provenance — every frame is traceable
  4. Budget honesty — cost is enforced, not estimated
  5. Consent enforcement — data-flow consent is an audit trail
  6. Conformance verification — contracts are machine-checked
  7. Citation guarantees — frames carry human labels
  8. Version stability — evolution without flag days
  9. Temporal validity — facts are time-pinned
  10. Why the combination is irreducible
  11. Comparison to alternatives
  12. Grounding in primary research

1. The problem: context as an unaccountable blob-pipe

Consider the retrieval path of a typical coding agent today. When the agent needs context about a codebase, it runs a search — a vector similarity query, a grep, a tree-sitter symbol lookup — and pastes the raw results into the prompt. The following questions have no answers:

  • How many tokens did this cost? The agent approximates or ignores it. There is no contract that the cost figure is honest.
  • Where did this come from? A file path, maybe. A line range, sometimes. A cryptographic digest, almost never. The agent cannot verify that the snippet it was given matches what is on disk.
  • Did this leave the machine? If the retrieval called a cloud embedding API or a remote search service, workspace content was transmitted. There is no record of consent for that egress.
  • Can I cite this in output? The agent would have to construct a citation from the raw blob. There is no guarantee the material carries a human-readable label.
  • Is this still valid? A fact retrieved from an embedding store may be stale — the underlying file changed since it was indexed. There is no temporal validity window.

This is the blob-pipe: retrieval as an opaque firehose of unverified, unaccountable text. It works well enough until it doesn't — until the budget silently overflows, until a provider lies about cost, until workspace content leaks to a third-party service without consent, until a stale fact sends the agent down a wrong path, until an auditor asks "where did this answer come from?" and there is no trail.

Context Graph Protocol exists to make every one of those questions answerable, not by convention, but by contract — a wire protocol whose invariants are enforced by the host runtime and verified by a public conformance suite.


2. The seven advantages

PropertyWhat it guaranteesEnforced by
ProvenanceEvery frame carries its full origin chain (URI, range, digest, method, agent)ContextFrame.provenance (contextgraph-types::frame)
Budget honestyA provider's frames never sum above the query's max_tokens; a lie is detected and the frames are droppedHost::query_one_isolated budget audit (contextgraph-host::host); frame-validity conformance check
Consent enforcementAn egress provider is never queried until recorded, named consent exists; the query payload is not transmitted before thatConsentStore::permits (contextgraph-host::consent); Host::query_provider gate
Conformance verification"Context Graph Protocol conformant" is a machine-checked claim, not a self-attestation; the conformance suite is adversarialcontextgraph-conformance — 5 checks that deliberately trip each failure mode
Citation guaranteesEvery frame has a non-empty title and citation_label; raw ids are never the primary identifierframe-validity conformance check; platform-wide convention
Version stabilityThe protocol evolves within a major family without breaking interop; the draft-to-freeze transition requires no flag dayversions_compatible (contextgraph-host::wire); major-family matching
Temporal validityFacts carry valid_from / valid_to windows; queries can pin retrieval to a point in time via as_ofContextFrame temporal fields; ContextQuery.as_of (contextgraph-types)

Each property is defined not by documentation but by a type in contextgraph-types and an enforcement path in contextgraph-host or contextgraph-conformance. The remainder of this document examines each in turn.


3. Provenance — every frame is traceable

A ContextFrame is not a string. It is a structured unit carrying its own origin chain:

pub struct Provenance {
    pub kind: String,       // "file", "derivation", "episode"
    pub uri: Option<String>,
    pub range: Option<String>,
    pub digest: Option<String>,   // cryptographic integrity check
    pub method: Option<String>,   // how it was produced
    pub by: Option<String>,       // which agent/tool produced it
}

A frame returned from a code-graph provider might carry a file provenance entry with uri: "file:///repo/src/lib.rs", range: "L120-160", and digest: "sha256:...". A frame derived from an agent's prior reasoning might carry a derivation entry with method: "tree-sitter-symbol-extraction" and by: "contextgraph-graph". A frame from an episodic memory store might carry an episode entry naming the session that produced it.

Why this matters. When a judge — whether an LLM judge in goal mode or a human reviewer — asks "where did this claim come from?", the answer is in the frame, not in a side channel that may or may not have been maintained. A host that quotes frame content into a prompt can simultaneously quote the provenance, giving the downstream model or human a verifiable trail rather than an assertion. This is the difference between retrieval and evidence.

The cryptographic digest field is particularly important for integrity guarantees: a frame's content can be checked against the digest to confirm it has not been tampered with or drift-corrupted in transit. In a system where a stale embedding index serves a snippet that no longer matches the file on disk, the digest mismatch is detectable before the frame enters a prompt.


4. Budget honesty — cost is enforced, not estimated

Every ContextQuery carries a max_tokens budget. The contract is absolute:

The frames a provider returns must sum token_cost to at most the query's max_tokens.

This is not a hint. It is not a best-effort suggestion. It is a conformance requirement, enforced at three levels:

  1. Self-check (ContextQueryResult::respects_budget) — any host or provider can run this cheap comparison.
  2. Host enforcement (Host::query_one_isolated) — the host runs respects_budget on every provider response during fan-out. A provider whose frames exceed the budget is classified as ProviderResult::BudgetLie, its frames are dropped, and the violation is surfaced via FanOut::budget_liars() — a loud, named report, never a silent discard.
  3. Conformance (budget-honesty check) — the conformance suite deliberately constructs a query with a tight budget and verifies the provider's response respects it.

Why this is qualitatively different from existing approaches. In a typical RAG pipeline, the retrieval step and the budget step are decoupled: the retriever returns "the top-K results," and the prompt assembler hopes they fit. When they don't, the assembler either truncates (losing the tail silently) or overflows (sending more tokens than budgeted, inflating cost and latency). In Context Graph Protocol, the budget is part of the query contract, and the provider is responsible for selecting its best frames within that budget — with truncated: true and dropped_estimate if it had more material than fit. The host never has to guess whether the retrieval step respected the budget; it can verify it in O(frames) and drop a liar's output entirely.

This is what makes composable fan-out safe. When a host queries five providers concurrently and composes their frames into a single prompt, each provider's budget share is enforced independently. One provider lying about costs cannot inflate the total beyond what the host allocated — its frames are dropped and reported, and the other four providers' frames compose honestly.


The DataFlow struct is the security-critical field in Context Graph Protocol:

pub struct DataFlow {
    pub reads: bool,   // can see workspace content via query payloads
    pub writes: bool,  // persists context/upsert writes
    pub egress: bool,  // sends anything off the local machine
}

The rule is absolute:

A conforming host MUST NOT auto-enable a provider that declares egress: true. It must gate that provider behind an explicit, one-time consent that names what leaves.

This is enforced structurally:

  • ConsentStore::requires_consent(info) returns true if and only if info.data_flow.egress is true. Read/write-only providers carry no gate.
  • ConsentStore::permits(id, info) is the gate the host consults before transmitting the query payload. An unconsented egress provider's query is never sent — the payload itself may carry workspace content, so it must not reach the provider before consent.
  • The ConsentRecord retains granted_scope — a human-readable description of what data flows out — as an auditable trail. Consent is not a boolean checkbox; it is a named, recorded, revocable decision.
  • contextgraph-host's HTTP transport goes further: it treats every remote provider as egress regardless of the handshake claim, so a remote provider cannot lie its way out of the consent gate.

Why this matters. In a world where coding agents increasingly integrate with external services — issue trackers, documentation APIs, cloud embedding stores, knowledge graphs — the question "what left my machine?" becomes critical for enterprise security, compliance, and trust. Context Graph Protocol's consent model makes this answerable at the protocol level: the consent store is a serde-able audit log that a security team can inspect, and the gate is enforced before data transmission, not after.

The distinction between reads (the provider sees workspace content in the query payload) and egress (the provider sends it off-machine) is subtle but essential. A local code-graph provider reads workspace content but has no egress — it is auto-enabled. A cloud documentation search provider has egress even if it doesn't read workspace content — it still requires consent because the query text itself may contain sensitive information.


6. Conformance verification — contracts are machine-checked

"Context Graph Protocol conformant" is not a self-attestation. It is a machine-checked claim, defined as green on contextgraph-conformance's suite for your declared capability set. The suite is deliberately adversarial:

CheckWhat it provesFails when
handshakeThe provider completes the handshake and reports a non-empty identity + capabilitiesThe handshake errors, times out, or name/version is empty
frame-validityEvery returned frame is citable and scored honestlyAny frame's score is outside [0, 1], or its title/citation_label is empty
budget-honestyThe provider never lies about token_costReturned frames' summed token_cost exceeds the query's max_tokens
shutdown-cleanThe provider tears down without errorshutdown errors or the provider vanishes before responding
malformed-input-toleranceA garbage line on the wire doesn't crash the providerThe provider dies (stdio only)

The suite is shipped with a --misbehave mode that deliberately trips each check — a provider that returns lying costs, emits an out-of-range score, omits a citation label, dies on garbage input, or crashes mid-query is caught with an evidence string naming the exact violation. This doubles as proof that the suite genuinely catches broken providers rather than rubber-stamping everything.

Why this matters. The alternative to a conformance suite is a documentation page that says "please be honest about costs and include citation labels." Documentation is advisory; conformance is verifiable. A third party building an Context Graph Protocol provider can run contextgraph-inspect in their CI and gate on it — the same binary, the same checks, that the reference host runs. This makes interoperability a testable property rather than a trust assumption.


7. Citation guarantees — frames carry human labels

Every frame has a non-empty title and a non-empty citation_label. This is not cosmetic; it is a conformance failure if either is empty or missing.

The platform-wide convention is explicit: raw ids are never the primary on-screen identifier. A frame's id is provider-scoped and stable for dedup; its title and citation_label are human-readable. When a host quotes frame content into a prompt, it can simultaneously produce a citation — "per [workspace.ts L120-160]" — without constructing one from metadata.

This matters for grounding and verifiability. An agent that claims "the authentication module handles token refresh" should be able to cite the frame that supports the claim — a frame with a title, a URI, a line range, and a provenance chain. Without mandatory citation labels, citations are best-effort and frequently absent, leaving the agent's claims ungrounded.

The Relation type extends this principle to graph edges: a relation's display_name ensures a graph edge is surfaced by human label, never a raw target id. The convention is consistent across the protocol surface.


8. Version stability — evolution without flag days

Context Graph Protocol separates crate version (ordinary Cargo semver) from protocol version (the wire-format identity negotiated at handshake). The current protocol version is contextgraph/1.0-draft.

Two protocol versions interoperate when they share a major family — the substring up to the first .. So contextgraph/1.0-draft and contextgraph/1.0 both belong to family contextgraph/1 and interoperate. A jump to contextgraph/2.0 does not.

This design has a critical consequence: the freeze from draft to stable does not require a flag day. When the protocol drops the -draft suffix and becomes contextgraph/1.0, every already-deployed 1.0-draft provider continues to handshake successfully — they share the 1 family. What breaks interop is reserved for a genuinely breaking redesign (contextgraph/2.0), which would require a new crate major version in lockstep.

Why this matters. A protocol that requires all participants to upgrade simultaneously is fragile — it creates coordination overhead and incentivizes freezing the spec to avoid disruption. Context Graph Protocol's major-family model allows incremental evolution within a family (additive fields, tighter checks) without breaking deployed providers, while reserving the major-version bump for real breaking changes. Early adopters who pin contextgraph-types = "=0.1.0" get a hard guarantee; those who use ^0.1 accept pre-1.0 churn but gain forward-compatibility within the family.


9. Temporal validity — facts are time-pinned

ContextFrame carries three temporal fields:

  • valid_from — when the fact became true.
  • valid_to — when it ceased to be true (if ever).
  • recorded_at — when the frame was captured.

And ContextQuery carries:

  • as_of — pin retrieval to a point in time.

Together, these enable bi-temporal retrieval: a query can ask "what was true about this function as of last Tuesday?" and receive only frames whose validity window includes that timestamp. This is the same discipline that bi-temporal databases (like Btrieve or Crux) apply to transactional data, applied here to the context that feeds an AI agent.

Why this matters. In a codebase under active development, a symbol definition retrieved from an embedding index may represent a version of the code that has since changed. Without temporal validity, the agent operates on stale information and may produce changes that conflict with the current state. With temporal validity, the host can detect staleness (the frame's valid_to is set, or its digest doesn't match the current file) and either refresh or discard it.

This is the property that makes Context Graph Protocol suitable for long-running, multi-session agents: context accumulated in one session carries temporal metadata that a future session can evaluate for continued relevance. Episodic memory — lessons learned in a prior task — can expire or be superseded, and the temporal fields make that lifecycle explicit rather than implicit.


10. Why the combination is irreducible

Each advantage above is individually valuable. But the claim of this document is stronger: the combination is irreducible. Removing any single property collapses the trust model.

Consider what happens if you remove each property in isolation:

  • Remove provenance. Frames become blobs. A host cannot verify where a frame came from, so it cannot detect drift, staleness, or fabrication. The agent operates on unverified assertions. → Back to the blob-pipe.

  • Remove budget honesty. A provider can silently overrun the budget. In a multi-provider fan-out, one lying provider inflates the total cost and latency, and the host has no way to detect which provider was responsible. Budget compositability breaks. → Unbounded cost.

  • Remove consent enforcement. Any provider can exfiltrate workspace content to a remote service. The "no phone-home" guarantee — central to Context Graph Protocol's trust model — becomes unenforceable at the protocol level. → Data leakage.

  • Remove conformance verification. "Context Graph Protocol conformant" becomes a self-attestation. Interoperability degrades to "works with the reference host" rather than "proven against a specification." Third-party adoption requires trust rather than verification. → Vendor lock-in via ambiguity.

  • Remove citation guarantees. Frames lack human labels. An agent's claims become ungrounded — it cannot point to the source of its assertion. Judge verification (in goal mode) cannot trace a claim to evidence. → Ungrounded output.

  • Remove version stability. Every protocol change is a flag day. Deployed providers break on upgrade. The protocol cannot evolve without coordinated migration. → Stagnation or chaos.

  • Remove temporal validity. Facts have no expiry. A stale embedding serves a snippet that no longer matches the code. The agent operates on outdated information. → Silent drift.

The properties compose. Provenance without budget honesty means you can trace a frame's origin but not control its cost. Budget honesty without consent means costs are honest but data may leak. Consent without conformance means the gate exists but is not verified. Each property closes a gap that another property does not address. This is why Context Graph Protocol is specified as an integrated protocol, not a menu of optional features.


11. Comparison to alternatives

vs. Model Context Protocol (MCP)

MCP (Anthropic, 2024) defines a protocol for connecting external tools and resources to LLM-based applications. Context Graph Protocol and MCP are complementary, not competing:

  • MCP connects tools — functions the agent can call (run a query, fetch a resource, execute a command). It is an action protocol.
  • Context Graph Protocol connects context — typed, budgeted, provenance-carrying frames that a host composes into a prompt before the model acts. It is a retrieval-evidence protocol.

MCP has no budget-honesty contract (a tool response has no token_cost field), no consent-gating for egress (tools are trusted to do what they declare), no provenance chain on responses, and no conformance suite that verifies these properties. These are not deficiencies in MCP — they are scope boundaries. MCP is designed for tool invocation; Context Graph Protocol is designed for evidence retrieval. An agent that needs both composes them: Context Graph Protocol providers feed context into the prompt, MCP tools execute actions.

The architectural distinction is that Context Graph Protocol frames are transported as untrusted data — a conforming host delimits frame content as quoted material, never as instructions. This is the same security principle that separates email body from email headers: the content of a retrieved frame is data the model reads, not a directive the model executes. MCP tool results are treated similarly by well-designed hosts, but Context Graph Protocol makes the untrusted-data contract part of the protocol specification rather than leaving it to host implementation.

vs. ad-hoc RAG pipelines

A typical RAG (Retrieval-Augmented Generation) pipeline retrieves chunks from a vector store and pastes them into the prompt. Compared to Context Graph Protocol:

  • No budget contract. The retriever returns top-K; the prompt assembler hopes they fit. Context Graph Protocol's max_tokens is part of the query and enforced.
  • No provenance. Chunks carry a source document, at best. Context Graph Protocol frames carry URI, range, digest, method, and agent.
  • No consent model. A cloud embedding API is called without gating. Context Graph Protocol gates egress behind recorded, named consent.
  • No conformance. There is no way to verify a RAG pipeline respects any contract. Context Graph Protocol defines conformance as machine-checked.
  • No temporal validity. Chunks are current-or-not, with no validity window. Context Graph Protocol frames carry bi-temporal metadata.

vs. vendor-locked retrieval

Some coding agents (Claude Code, Cursor, Windsurf) integrate tightly with a vendor's proprietary retrieval or indexing service. The retrieval is opaque, the provider is the vendor, and the user has no visibility into cost, provenance, or consent. Context Graph Protocol inverts this: retrieval is an open protocol, the provider is pluggable (in-process, stdio, HTTP — any language), and the contracts are public and machine-checked.


12. Grounding in primary research

The design of Context Graph Protocol is grounded in research on retrieval-augmented generation, context window economics, and software-engineering agent architecture:

  • Lost in the Middle (Liu et al., TACL 2024, arXiv:2307.03172) — demonstrates that LLM performance degrades when relevant information is buried in long contexts. Context Graph Protocol's budget-honesty contract and frame-level relevance scoring are directly motivated by this: a host that can trust per-frame cost and score can compose a prompt that places the most relevant evidence at the attention surface, rather than stuffing an unaccountable blob.

  • Context Rot (Hong et al., Chroma, 2025, research.trychroma.com) — shows that increasing input tokens degrades LLM performance even when the additional tokens are relevant. This validates Context Graph Protocol's position that more context is not betterhonest, budgeted, provenance-carrying context is better. The max_tokens contract is not just about cost; it is about preventing context rot.

  • Graph RAG (Edge et al., Microsoft Research, 2024, arXiv:2404.16130) — demonstrates that graph-structured retrieval (entity-relationship summarization) outperforms flat vector search for global questions about a corpus. Context Graph Protocol's Relation type and FrameKind::Graph are designed to carry graph-structured context natively — a provider can return frames with typed relations, not just text chunks.

  • Repo map with tree-sitter (Gauthier, Aider, 2023, aider.chat) — shows that a tree-sitter-derived repository map (symbols + import edges) gives an agent structural awareness that grep cannot. Context Graph Protocol's FrameKind::Symbol and provenance method: "tree-sitter-symbol-extraction" are designed for exactly this kind of structural frame.

  • AI Agents That Matter (Kapoor et al., Princeton, TMLR 2025, arXiv:2407.01502) — argues that agent benchmarks must report cost, not just accuracy, because a system that is more accurate but 10x more expensive is not necessarily better. Context Graph Protocol's token_cost field and budget-honesty contract make cost a first-class, auditable property of every context exchange.

  • MemGPT (Packer et al., UC Berkeley, 2023, arXiv:2310.08560) — proposes an operating-system-like memory hierarchy for LLMs (main context vs. external context). Context Graph Protocol's frame types (Memory, Episode, Fact) and temporal validity windows are the wire-level expression of this hierarchy: different kinds of memory with different lifecycles, all flowing through one typed protocol.


Summary

The Context Graph Protocol is not a faster retrieval pipeline or a richer embedding model. It is a trust architecture for context: a protocol-level guarantee that every frame entering an agent's prompt is traceable to its source (provenance), honest about its cost (budget), gated by recorded consent (consent), verified against a specification (conformance), citable by human label (citation), stable across evolution (version), and valid as of a known time (temporal). No prior retrieval protocol combines all seven properties, and the combination is irreducible — each property closes a gap the others do not address.

The implementation is open (contextgraph-types, contextgraph-host, contextgraph-conformance), dual-licensed MIT OR Apache-2.0, zero-dependency beyond serde for the wire types, and conformance-verified today. The path from "open context as an idea" to "open context as a standard" is the conformance suite: anyone can build a provider, anyone can verify it, and the protocol evolves within a stable family without flag days.


See also: Protocol surface for the wire types, Implementing a provider for the build guide, Running conformance for the verification suite, and Stability for the version model.

On this page