Please enable JavaScript to use CodeHS

How to Build an Agent Harness

Tools and memory

A real harness needs more than a stubbed model. It needs a tool registry so the LLM can call real functions, and a memory system so it can remember what it learned across many steps.

Python ~12 min Advanced
The Big Idea

From stub to real tools

In the last tutorial, the dispatch function was hard-coded. A real harness needs to add tools without rewriting the core loop. The solution is a tool registry: a dictionary that maps tool names to Python functions and their schemas.

Each tool is a function that takes arguments, does something, and returns a plain Python object. The harness does not care what the tool does; it only cares about the name, the arguments, and the result.

Key idea: tools are plugins. The loop stays the same even when you add new tools.


Code

A tool registry

Below is a real ToolRegistry and three tools: read_file, write_file, and run_command. The harness can now read, write, and run code safely.


Code

Memory and context

An LLM can only read a fixed number of tokens at once, called its context window. If a conversation gets too long, older messages must be dropped or summarized. A Memory class wraps that logic.


Interactive

Watch memory shrink

Add messages until the memory reaches its limit. When it trims, older messages collapse into a summary so the model still has some idea what happened.


Your Turn

Check yourself

Why is it important that run_command has a timeout argument, and why should the harness not let the model run arbitrary shell commands?

Your answer:

Sample answer: A timeout prevents a command from hanging forever. The harness should limit which commands can run, or run them in an isolated sandbox, because an LLM could generate dangerous commands that delete files or leak data.


Check Yourself

Quick quiz

1. What does the ToolRegistry do?

2. Why does memory need a max_messages limit?

3. What is a context window?

Keep learning

Tools and memory give the harness hands and a notebook. Next, wire them together into a coding agent that can read, edit, and test real code.

Part of the “How to Build an Agent Harness” series.