Context Engineering: Information for Agents
"The delicate art and science of filling the context window with just the right information" — Andrej Karpathy on context engineering
Context engineering is curating and maintaining the optimal set of tokens (information) during LLM inference. It’s about finding the smallest possible set of high-signal tokens that maximize the likelihood of desired outcomes.
In Spring AI Agents, context is provided through the Advisor pattern, following the same approach as ChatClient.
For a comprehensive treatment of context engineering principles, see Anthropic’s Effective Context Engineering for AI Agents blog post.
The Challenge
A major challenge with autonomous agents is context growth. A typical agent task requires ~50 tool calls (Manus AI), and as context grows:
-
Performance drops (Chroma research on "context rot")
-
Costs increase (every token in context costs money)
-
Latency increases (more tokens to process)
-
Quality degrades (LLMs struggle with very long contexts)
CLI Agents and File System Context
CLI agents solve context management through the file system, implementing key context engineering techniques:
Just-in-Time Context Retrieval
Instead of pre-loading all information, agents:
-
Maintain lightweight identifiers - Keep references to files/resources, not full content
-
Dynamically load relevant information - Read files only when needed for the current step
-
Explore and discover progressively - Build context as the task unfolds
Structured Note-Taking
Agents write and reference external notes:
-
Write context to files - Instead of keeping everything in LLM memory, agents write
todo.md
, research plans, intermediate results to files -
Persistent state - File system provides durable storage across agent iterations
-
Compaction - Summarize and consolidate information to reset context windows
This is context engineering in practice.
Real-World Examples
Examples from production agentic systems:
-
Manus AI: Writes
todo.md
at task start, rewrites during execution. Recitation of objectives helps maintain focus without bloating context. -
Anthropic multi-agent researcher: Writes research plans to files so they can be retrieved as needed
-
Cursor/Windsurf: Use sophisticated retrieval to assemble relevant code into prompts without overwhelming context
CLI agents naturally do context engineering because they operate through bash and file systems—reading, writing, searching files is their native mode.
Providing Context with Advisors
In Spring AI Agents, context is provided using Advisors:
AgentClientResponse response = agentClient
.goal("Increase JaCoCo test coverage to 80%")
.workingDirectory(projectRoot)
.advisors(vendirAdvisor, judgeAdvisor) // ← Context providers
.run();
Example: VendirContextAdvisor
The VendirContextAdvisor
pulls documentation into the workspace so agents can access it:
VendirContextAdvisor vendirAdvisor = VendirContextAdvisor.builder()
.include("https://github.com/jacoco/jacoco//docs", "jacoco-manual")
.build();
This downloads the JaCoCo manual into the workspace. The agent can then read it to understand coverage metrics and requirements.
See VendirContextAdvisor documentation for complete usage details and examples.
Context Best Practices
-
Provide just enough - Too little context and the agent guesses; too much and performance degrades
-
Use files for large content - Write documentation, schemas, examples to files rather than prompt text
-
Scope appropriately - Only include information relevant to the goal
-
Let agents manage state - CLI agents naturally write intermediate results to files
Next Steps
-
Goals - Defining what agents should accomplish
-
Judges - Verifying agent work
-
CLI Agents - Understanding autonomous agents
-
Getting Started - Build your first agent