Chapter 9 Context Engineering
In previous chapters, we have introduced memory systems and RAG for agents. However, to enable agents to stably "think" and "act" in real complex scenarios, memory and retrieval alone are not enough—we need an engineering methodology to continuously and systematically construct appropriate "context" for the model. This is the theme of this chapter: Context Engineering. It focuses on "how to assemble and optimize input context in a reusable, measurable, and evolvable way before each model call", thereby improving correctness, robustness, and efficiency<sup>[1][2]</sup>.
To enable readers to quickly experience the complete functionality of this chapter, we provide a directly installable Python package. You can install the version corresponding to this chapter with the following command:
pip install "hello-agents[all]==0.2.8"This chapter mainly introduces the core concepts and practices of context engineering, and adds a context builder and two supporting tools to the HelloAgents framework:
- ContextBuilder (
hello_agents/context/builder.py): Context builder that implements the GSSC (Gather-Select-Structure-Compress) pipeline, providing a unified context management interface - NoteTool (
hello_agents/tools/builtin/note_tool.py): Structured note tool that supports persistent memory management for agents - TerminalTool (
hello_agents/tools/builtin/terminal_tool.py): Terminal tool that supports file system operations and just-in-time context retrieval for agents
These components together constitute a complete context engineering solution, which is key to implementing long-term task management and agentic search, and will be introduced in detail in subsequent sections.
In addition to installing the framework, you also need to configure the LLM API in .env. The examples in this chapter mainly use large language models for context management and intelligent decision-making.
After configuration is complete, you can start the learning journey of this chapter!