The Five Components That Constitute Context in an LLM API Call for an Agent
In the bojieli/ai-agent-book architecture, every LLM API call for an agent is built from five distinct components: the System Prompt, Tool Definitions, User Messages, Assistant Messages, and Tool Results, which together form a static prefix and a dynamic trajectory that enables robust multi-turn reasoning.
Building effective AI agents requires precise control over what the language model sees during each inference cycle. According to the source code and documentation in bojieli/ai-agent-book, the five components that constitute context in an LLM API call for an Agent are split between immutable configuration and evolving conversation state. This architecture determines how agents maintain state, invoke tools, and generate coherent responses across extended interactions.
The Architecture: Static Prefix vs. Dynamic Trajectory
The context window in an agent system is not monolithic. As defined in book-en/chapter1.md (lines 149-167), the five components are organized into two functional groups:
- Static Prefix: Components that remain constant across API calls, defining the agent's identity and capabilities
- Trajectory: Components that grow with each interaction, representing the evolving state of the conversation
This division ensures that the model receives both persistent instructions (the prefix) and current situational awareness (the trajectory) on every call.
Component 1: System Prompt (Static)
The System Prompt provides the agent’s identity, high-level instructions, and immutable behavioral rules. According to book-en/chapter1.md at line 167, this is delivered via the [System] message role and establishes the foundation for role-aware agent behavior.
This component never changes during a conversation session, ensuring consistent personality and constraint enforcement regardless of how long the interaction continues.
Component 2: Tool Definitions (Static)
Tool Definitions declare the functions, APIs, or capabilities the agent can invoke. As documented in book-en/chapter2.md (lines 56-58), these are delivered via the top-level tools field in the request body, separate from the message array.
These definitions include JSON schemas describing function signatures, required parameters, and descriptions that help the model understand when and how to invoke external capabilities. Like the system prompt, tool definitions typically remain static throughout a session.
Component 3: User Messages (Trajectory)
User Messages represent the human operator’s latest query or ongoing conversation input. This component is part of the trajectory that expands with each turn, as noted in book-en/chapter1.md at line 167.
Each new user input appends to this sequence, providing the model with the current objective or question that requires processing.
Component 4: Assistant Messages (Trajectory)
Assistant Messages contain the agent’s prior replies, including any Chain-of-Thought (CoT) reasoning or tool-call statements generated in previous turns. This trajectory component, referenced at line 167 of book-en/chapter1.md, allows the model to maintain context about what it has already said or decided.
These messages may include function_call objects indicating which tools the agent previously attempted to invoke, critical for maintaining logical continuity across multi-step reasoning chains.
Component 5: Tool Results (Trajectory)
Tool Results carry the outputs returned by invoked tools—such as search results, database queries, or calculation returns. As the third element of the trajectory identified in book-en/chapter1.md (line 167), these messages typically use the role: "tool" designation and contain the actual data the agent requested in previous steps.
Without this component, the agent would have no mechanism to incorporate external data into its reasoning process.
Implementing All Five Components in API Calls
When constructing an LLM API request for an agent, you must assemble both the static prefix and the trajectory. The following examples demonstrate the complete five-component structure as implemented in the bojieli/ai-agent-book codebase.
Python Implementation (OpenAI-Compatible)
import json
import openai
# 1️⃣ System prompt (static prefix)
system_prompt = {
"role": "system",
"content": "You are a helpful assistant that can search the web and summarize articles."
}
# 2️⃣ Tool definitions (static prefix)
tools = [
{
"type": "function",
"function": {
"name": "web_search",
"description": "Search the web for a query.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"]
},
},
}
]
# 3️⃣ User message (trajectory)
user_msg = {"role": "user", "content": "Find the latest research on quantum‑resistant cryptography."}
# 4️⃣ Assistant message (trajectory) – from a prior turn
assistant_msg = {
"role": "assistant",
"content": "Sure, let me look that up for you.",
"function_call": {"name": "web_search", "arguments": json.dumps({"query": "quantum‑resistant cryptography"})}
}
# 5️⃣ Tool result (trajectory) – result of the previous function call
tool_result = {
"role": "tool",
"name": "web_search",
"content": "Top result: https://example.com/qc‑paper.pdf – a 2024 paper on lattice‑based cryptography."
}
payload = {
"model": "gpt-4o-mini",
"messages": [system_prompt, user_msg, assistant_msg, tool_result],
"tools": tools,
"temperature": 0.2,
}
response = openai.ChatCompletion.create(**payload)
This implementation maps the four message roles (system, user, assistant, tool) plus the tools field to the five components described in book-en/chapter2.md.
Raw JSON Payload Structure
{
"model": "gpt-4o-mini",
"tools": [
{
"type": "function",
"function": {
"name": "weather",
"description": "Get current weather for a city.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"]
}
}
}
],
"messages": [
{"role": "system", "content": "You are a weather‑aware assistant."},
{"role": "user", "content": "What's the temperature in Paris today?"},
{"role": "assistant", "content": "Let me check that for you.", "function_call": {"name":"weather","arguments":"{\"city\":\"Paris\"}"}},
{"role": "tool", "name": "weather", "content": "Paris: 12°C, partly cloudy."}
],
"temperature": 0.0
}
The JSON structure explicitly separates the Tool Definitions (top-level tools array) from the message-based components, aligning with the architecture described in slides/lesson-07.md regarding context window composition.
Why These Five Components Are Essential
According to the ablation study referenced in book-en/chapter1.md, removing any one of these five components (except the system prompt, which is essential for basic role awareness) degrades agent performance. The static prefix ensures the model understands its capabilities and constraints, while the trajectory enables step-by-step reasoning and incorporation of external data.
The slides/lesson-03.md file emphasizes that omitting Tool Results breaks the agent's ability to utilize external knowledge, while missing Assistant Messages disrupts the logical chain of multi-turn problem solving.
Summary
- System Prompt: Establishes agent identity and immutable rules via the
systemrole - Tool Definitions: Declares available functions via the top-level
toolsfield - User Messages: Captures current human input via the
userrole - Assistant Messages: Preserves agent reasoning history via the
assistantrole - Tool Results: Incorporates external data returns via the
toolrole
Together, these five components form the complete context mechanism that powers robust agent architectures.
Frequently Asked Questions
What happens if I omit the Tool Results component from the API call?
Removing Tool Results severs the feedback loop between the agent and external tools. As documented in book-en/chapter1.md, the model will not see the output of any functions it calls, effectively rendering those tools useless and preventing the agent from incorporating real-time data into its responses.
How do the five components map to OpenAI's API structure?
According to book-en/chapter2.md (lines 56-58), the mapping follows a "four message roles + tools field" pattern: the System Prompt, User Messages, Assistant Messages, and Tool Results correspond to message roles, while Tool Definitions occupy the separate tools array in the request body.
Can the System Prompt and Tool Definitions change during a conversation?
Technically possible but architecturally discouraged. These components form the static prefix that defines the agent's consistent identity and capabilities across the session. Changing them mid-conversation can lead to inconsistent behavior, though advanced implementations in slides/lesson-07.md suggest dynamic tool registration for specific use cases.
Why are Assistant Messages necessary if I already have User Messages?
Assistant Messages preserve the agent's own reasoning steps and tool-call attempts, which is essential for maintaining logical continuity. Without this trajectory component (specifically the function_call data), the model cannot reference its previous decisions or understand which tools it has already invoked, leading to redundant or contradictory actions.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →