# Agent = LLM + Context + Tools: The Core Formula for Building Autonomous AI Agents

> Discover the core formula for building autonomous AI agents. Learn how LLM, Context, and Tools combine to create powerful AI systems capable of reasoning, remembering, and acting.

- Repository: [Bojie Li/ai-agent-book](https://github.com/bojieli/ai-agent-book)
- Tags: deep-dive
- Published: 2026-08-23

---

**An autonomous AI agent consists of three essential building blocks: a Large Language Model (LLM) that reasons and generates output, a Context system that provides prompts, memory, and retrieval capabilities, and Tools that enable the agent to interact with external systems and execute actions.**

The book *AI Agents in Depth* (maintained in the `bojieli/ai-agent-book` repository) establishes this architecture as the foundational paradigm for modern agentic systems. According to the project overview in [`README.md`](https://github.com/bojieli/ai-agent-book/blob/main/README.md) (lines 10-11), the formula **Agent = LLM + 上下文 + Tools** represents the minimal set of components required to transform a static language model into an autonomous agent capable of planning, reasoning, and acting upon the world.

## Breaking Down the LLM + Context + Tools Architecture

### LLM (Large Language Model)

The **LLM** serves as the central generative engine and reasoning core of the agent. It interprets user instructions, processes contextual information, and decides which actions to take. As implemented in the codebase, the `LLMAgent` class (found in [`tests/test_ch1_search_codegen_null_response.py`](https://github.com/bojieli/ai-agent-book/blob/main/tests/test_ch1_search_codegen_null_response.py) lines 26-33) wraps the underlying model and exposes methods like `_tool_items`, `_output_text`, and `_citations` to manage the decision loop.

Typical implementations include OpenAI GPT-4, Anthropic Claude, DeepSeek, and Qwen. The LLM does not operate in isolation; it requires the other two components to function as an agent rather than a simple chatbot.

### Context (Prompt + Memory + Retrieval)

**Context** provides the information substrate the LLM needs to act intelligently. This component encompasses static system prompts, dynamic conversation history, user-specific memory modules, and externally retrieved knowledge. The architecture supports various retrieval mechanisms including KV-cache optimization, RAG (Retrieval-Augmented Generation) pipelines, and structured document indexes.

In [`book/chapter1.md`](https://github.com/bojieli/ai-agent-book/blob/main/book/chapter1.md), the authors emphasize that context management distinguishes stateless APIs from stateful agents. The context layer feeds into the LLM's input window, grounding its generations in relevant facts and conversation history rather than just parametric knowledge.

### Tools (External Functions)

**Tools** extend the agent's capabilities beyond text generation, allowing it to execute code, query databases, browse the web, or control devices. These are callable functions—typically HTTP APIs, code interpreter sandboxes, or specialized actuators—that the LLM can invoke via structured outputs. The repository's test suite demonstrates this through classes like `WebSearchTool` and `CodeInterpreterTool`.

The tooling interface is detailed in [`book/chapter4.md`](https://github.com/bojieli/ai-agent-book/blob/main/book/chapter4.md), which covers tool discovery mechanisms, execution semantics, and safety checks. This layer transforms the LLM from a passive predictor into an active system capable of affecting external state.

## How the Components Interact in Code

The repository provides concrete Python implementations demonstrating how these three pillars integrate. The following examples use the `LLMAgent` abstraction from the test suite ([`tests/test_ch1_search_codegen_null_response.py`](https://github.com/bojieli/ai-agent-book/blob/main/tests/test_ch1_search_codegen_null_response.py)).

### Standalone LLM without Context or Tools

```python
from llm_agent import LLMAgent

llm = LLMAgent(api_key="demo-key")
answer = llm.generate("Explain why the sky is blue.")
print(answer)

```

### Adding Context Through Retrieval

```python
from llm_agent import LLMAgent
from retrieval import SimpleRetriever  # RAG helper used in chapter 3 tests

retriever = SimpleRetriever()
context = retriever.search("sky color physics")  # returns relevant passages

prompt = f"{context}\nQuestion: Why is the sky blue?"
answer = llm.generate(prompt)
print(answer)

```

### Full Agent with Tools

```python
from llm_agent import LLMAgent
from tools import WebSearchTool, CodeInterpreterTool

search = WebSearchTool()
code = CodeInterpreterTool()

agent = LLMAgent(
    api_key="demo-key",
    tools=[search, code],
    system_prompt="You are a helpful assistant that can browse the web and run Python code."
)

result = agent.run(
    "Find the latest Python 3.12 release notes and extract the list of new language features."
)

# The LLM decides to call WebSearchTool, then may invoke CodeInterpreterTool to parse results

print(result)

```

## Key Source Files Defining the Formula

The **Agent = LLM + Context + Tools** paradigm is documented across several critical files in the `bojieli/ai-agent-book` repository:

- **[`README.md`](https://github.com/bojieli/ai-agent-book/blob/main/README.md)** (lines 10-11 and 58-63): Contains the explicit formula statement **Agent = LLM + 上下文 + Tools** and the chapter overview table establishing this as the foundational concept
- **[`book/chapter1.md`](https://github.com/bojieli/ai-agent-book/blob/main/book/chapter1.md)**: Introduces the three components and explains why each is essential for autonomy
- **[`tests/test_ch1_search_codegen_null_response.py`](https://github.com/bojieli/ai-agent-book/blob/main/tests/test_ch1_search_codegen_null_response.py)** (lines 26-33): Demonstrates the `LLMAgent` implementation showing how `_tool_items` and output handlers wire together the triad
- **[`scripts/gen_og_card.py`](https://github.com/bojieli/ai-agent-book/blob/main/scripts/gen_og_card.py)** (lines 39-40): Embeds the formula in the project's social preview metadata, reinforcing it as the core architectural thesis
- **[`book/chapter4.md`](https://github.com/bojieli/ai-agent-book/blob/main/book/chapter4.md)**: Details the Tool layer implementation, including discovery protocols and execution safety

## Summary

- **Agent = LLM + Context + Tools** is the foundational architectural formula for autonomous AI systems defined in the *AI Agents in Depth* book.
- The **LLM** acts as the reasoning engine, processing instructions and deciding which actions to take based on the provided context.
- **Context** encompasses prompts, conversation history, and retrieved knowledge, providing the situational awareness necessary for intelligent behavior.
- **Tools** are executable functions that allow the agent to interact with external APIs, run code, and manipulate real-world systems beyond generating text.
- The `bojieli/ai-agent-book` repository implements this triad through the `LLMAgent` class and associated tooling abstractions in its test suite and documentation.

## Frequently Asked Questions

### What distinguishes Context from Tools in the Agent formula?

**Context** provides information *to* the LLM (prompts, memory, retrieved documents), while **Tools** are capabilities that allow the LLM to act *upon* the world (APIs, code execution). According to [`book/chapter1.md`](https://github.com/bojieli/ai-agent-book/blob/main/book/chapter1.md), context is input data that shapes the LLM's reasoning, whereas tools represent output actions that can change external system states or fetch live data not present in the training set.

### Can an AI agent function without the Tools component?

Yes, but it ceases to be an autonomous agent and becomes a conversational assistant. As shown in the first code example using bare `LLMAgent.generate()`, an LLM with only Context can answer questions based on provided information but cannot verify facts against live web sources or execute code to perform calculations. The Tools layer is what enables the agent to overcome knowledge cutoffs and interact with dynamic environments.

### How does the LLM decide which Tool to use?

The LLM analyzes the user request and available context, then generates structured output (typically JSON or function calls) specifying the tool name and arguments. In [`tests/test_ch1_search_codegen_null_response.py`](https://github.com/bojieli/ai-agent-book/blob/main/tests/test_ch1_search_codegen_null_response.py), the `LLMAgent` class exposes `_tool_items` to track available functions, and the LLM selects from this registry based on the task requirements. The decision logic is governed by the system prompt and in-context examples provided in the Context component.

### Where is the Agent = LLM + Context + Tools formula explicitly defined in the source code?

The formula appears explicitly in [`README.md`](https://github.com/bojieli/ai-agent-book/blob/main/README.md) at line 10, which states **Agent = LLM + 上下文 + Tools** (where 上下文 means Context), and is reiterated in the chapter overview table at lines 58-63. It is also embedded in the project's social metadata within [`scripts/gen_og_card.py`](https://github.com/bojieli/ai-agent-book/blob/main/scripts/gen_og_card.py) at lines 39-40, confirming its status as the central architectural thesis of the repository.