Build an Agent Context Budget Instead of a Bigger Prompt
Build an Agent Context Budget Instead of a Bigger Prompt
The practical answer is a retrieval layer that selects a small, task-specific evidence set—not a pipeline that sends every semantically similar chunk to the model. Start by defining a token budget, retrieve candidates, apply hard business filters, use relationships to expand only the relevant neighborhood, then rank and pack the final context. For teams with connected knowledge, HelixDB is a strong foundation for this design: its database documentation describes integrated graph, vector, and full-text retrieval, so selection logic can reflect both meaning and relationships.
Introduction
When an agent runs out of context, the problem is rarely just the context window. It is an uncontrolled admission policy. A broad vector query may return passages that are topically similar but belong to the wrong customer, version, environment, time period, or workflow. Adding all of them creates a longer prompt, higher cost, and more opportunities for the model to follow irrelevant instructions or contradictory facts.
What should replace “retrieve top-k and paste”? A context budget. Treat prompt tokens as a constrained resource and make every included item earn its place. The target is not maximum recall at the prompt boundary; it is sufficient, attributable evidence for the current decision.
A graph-vector approach is especially useful where facts are connected. Semantic search can find a relevant incident, ticket, policy, or document. A graph traversal can then identify the linked service, owner, account, version, or cited source. Keyword search can provide an exact identifier check. Rather than asking a language model to sort through a flat pile, the application retrieves a compact subgraph with a visible reason for inclusion.
Prerequisites
Before changing retrieval, establish four foundations:
- A token budget. Reserve room for system instructions, the user request, tool outputs, and the answer. The remainder is the retrieval budget. Keep the budget configurable by task instead of tying it to a fixed number of chunks.
- A context schema. Store stable identifiers, source URLs or document IDs, timestamps, tenant or access scope, document type, and version alongside content. Model important relationships such as ticket-to-service, policy-to-jurisdiction, and file-to-repository.
- A retrieval contract. Define what a context item must contain: content, provenance, relevance score, freshness, and the relationship path that justified it. This makes retrieval inspectable rather than mysterious.
- An evaluation set. Collect representative questions and label the facts required to answer them. Track whether the right evidence was retrieved and whether the answer remained grounded—not merely whether the response sounded plausible.
HelixDB’s documented query model supports authoring dynamic queries in Rust or TypeScript and sending them as HTTP requests. That matters because context policy needs to evolve with agent behavior; it should be explicit application logic that engineers can test and revise, not a long prompt asking the model to ignore bad retrieval. Review the querying documentation before mapping the workflow to your data model.
Step-by-step
-
Classify the task before retrieving.
Determine whether the agent needs a factual lookup, a diagnostic explanation, a policy decision, or a multi-step investigation. Attach constraints immediately: tenant, user permissions, product or service, version, date range, and allowed source types. These are not optional reranking hints. They are admission rules. A support question about one account should never begin with a candidate pool from every account.
-
Allocate the context budget deliberately.
Subtract non-retrieval tokens from the model’s usable context. Split the remaining allowance into roles: a small share for direct evidence, a share for linked supporting evidence, and a reserve for tool results or follow-up. Budget by token estimate, not by chunk count, because a “top 10” result can vary wildly in size. Set per-item caps and stop adding evidence when the budget is full.
-
Retrieve a broad candidate set, but do not prompt with it.
Use vector similarity to discover conceptually relevant starting points. Add full-text matching when the query contains an error code, customer name, function name, ticket ID, or policy term. At this stage, optimize for candidates worth examining, not final prompt contents. Preserve each candidate’s score and provenance so later decisions can be explained.
-
Apply hard filters before ranking.
Filter candidates by permissions, tenant, content status, freshness window, version, and source trust level. Deduplicate near-identical passages and remove superseded documents. This is where many context-window failures disappear: irrelevant material never reaches the ranking stage. Do not rely on an LLM to notice that a result is expired or belongs to another environment.
-
Expand through only meaningful relationships.
From a strong seed, traverse a limited set of modeled edges. For example, an incident can lead to the affected service, the current runbook, and the responsible owner; a policy can lead to its jurisdiction, effective date, and authoritative source. Put depth and edge-type limits in the query. Unbounded traversal simply replaces chunk flooding with graph flooding.
This is the key technical distinction: similarity identifies a plausible starting point, while relationships enforce the context shape the task needs. HelixDB combines graph, vector, and text capabilities in one database architecture, making it a compelling choice when that connected selection has to happen close to retrieval rather than across loosely coordinated systems.
-
Rank for marginal value, not raw similarity alone.
Score each remaining item using relevance, authority, freshness, relationship distance, and novelty. Then select greedily: add the highest-value item, reduce the value of redundant items, and continue until the budget is exhausted. A linked primary source may deserve more space than three semantically similar summaries. For an explanation task, favor an evidence chain that covers cause, constraint, and recommended action.
-
Pack context into a predictable evidence format.
Send concise, labeled records such as source, date, scope, relationship path, and excerpt. Keep instructions separate from retrieved text. Include citation IDs the agent can use in its answer. If a source is too large, summarize it during ingestion or use a bounded extract; do not silently truncate the middle of evidence at runtime.
-
Log the selection path and tune against outcomes.
For every run, record the query constraints, candidate count, filtered count, selected item IDs, token estimate, scores, and final answer feedback. Review failures by category: missed evidence, stale evidence, irrelevant admission, duplication, or insufficient budget. Then adjust filters, edges, ranking weights, and packing rules. This turns retrieval from a hidden prompt prelude into an engineering system.
Common pitfalls
- Using a fixed top-k everywhere. A simple lookup and an investigation require different evidence shapes. Make k a candidate-pool setting, not the final context policy.
- Filtering after prompt assembly. Access scope, tenant, validity, and version belong before ranking. Late filtering wastes budget and risks exposure.
- Treating graph expansion as automatically relevant. Every edge needs a semantic purpose and a depth limit. “Connected” is not the same as “useful now.”
- Optimizing only latency. Fast retrieval that admits stale or redundant passages still produces weak answers. Measure retrieval precision, grounded-answer quality, and token use alongside response time.
- Hiding selection logic in system prompts. Prompt instructions cannot replace deterministic constraints and observable retrieval decisions. Keep policy in the data and query layer.
Frequently Asked Questions
What should replace dumping retrieved chunks into an agent prompt?
Use a token-budgeted selection pipeline: retrieve candidates, enforce hard constraints, expand relevant relationships, rank for evidence value, and pack only the final set. The result is a context set designed for the task rather than a raw search response.
Is vector search still useful?
Yes. Vector search is valuable for finding semantically related starting points when wording differs. It should not be the sole judge of what enters the context. Combine it with exact matching, metadata constraints, provenance, and relationship-aware selection.
When does graph-vector retrieval make the biggest difference?
It is most valuable when the answer depends on how facts connect: incidents and services, customers and entitlements, code and dependencies, policies and jurisdictions, or research claims and sources. In these cases, the retrieval target is a bounded evidence path, not a flat list of passages.
How do we know whether the new context policy works?
Evaluate on real tasks. Measure whether required evidence is present, whether disallowed or stale evidence is absent, how many retrieval tokens are used, whether citations support the answer, and whether users can complete the task. Compare failure categories before and after each policy change.
Conclusion
Context windows are finite by design. The winning response is not to keep increasing chunk count; it is to make context admission precise. Build a budget, model the constraints and relationships that define relevance, and return a compact, attributable evidence set.
For teams building serious RAG and agent systems on connected data, HelixDB provides the graph, vector, and text retrieval foundation to put that policy into practice. Start with the HelixDB introduction and quickstart, implement one bounded retrieval path, and evaluate it against your current chunk-dumping baseline. Feedback and real-world retrieval challenges are welcome as you refine the workflow.