Context Assembly & Prompt Management
Context assembly is arguably the most critical phase of the agent runtime. It determines what the model knows for any given turn — and getting it wrong leads to hallucinations, irrelevant responses, or blown token budgets.
The Four Components of Context
Every agent context window is assembled from four key sources:
- System Prompt: The foundational instructions that define the agent's personality, capabilities, constraints, and behavioral rules. This is typically static but may include dynamic elements like the current date or user preferences.
- Session History: The conversation so far — user messages, assistant responses, and tool call results. As conversations grow long, the runtime must decide what to keep, what to summarize, and what to drop.
- Memory Retrieval: Relevant information pulled from long-term storage. This includes vector-searched knowledge, past conversation summaries, and user-specific context. The key challenge is retrieving what's relevant without exceeding token limits.
- Skill Injection: Tool definitions, function schemas, and usage instructions that tell the model what tools are available. Smart runtimes selectively inject only relevant skills per turn rather than including every tool definition every time.
Token Budget Management
With context windows ranging from 4K to 200K+ tokens, managing the token budget is essential:
- Priority-Based Allocation: System prompt gets first priority, then recent history, then memory, then skills
- Sliding Window: Keep the most recent N messages in full, summarize older ones
- Relevance Scoring: Use embeddings to score which memory chunks are most relevant to the current query
- Compression: Summarize long tool outputs and previous conversation segments
Dynamic Context Strategies
- Just-In-Time Injection: Only load context when it is needed — don't preload everything
- Hierarchical Summarization: Create summaries at different levels of detail (recent = full, older = summary, oldest = key facts only)
- Skill Discovery vs. Skill Injection: First determine which skills are relevant (discovery), then inject only those skill definitions (injection). This keeps prompts lean and improves model output quality.
Common Pitfalls
- Context Stuffing: Filling the entire window degrades model performance — leave headroom for the response
- Lost in the Middle: LLMs pay less attention to information in the middle of long contexts. Place critical info at the start or end.
- Stale Context: Old conversation history can mislead the model. Aggressively prune or summarize outdated context.
- Tool Definition Bloat: Including 50+ tool definitions when only 3 are relevant wastes tokens and confuses the model.