Building a Production Agent Harness in LangChain
Part 1: Tools, structured output, runtime context, and tracing, in one small agent.
An agent can look complete surprisingly quickly:
- The model has a question and a few tools.
- Each tool result gets sent back to the model.
- And stop when the model produces an answer.
The loop fits on one screen of Python.
The first time running it, it feels like the hard part is already over. Ask why checkout is slow, and the agent searches logs, reads a runbook, and returns a useful answer.
Then, say, you change the question.
The agent asks for services/checkout/flag.py. The real file is flags.py, so the tool reports that the file does not exist. The agent ignores the error and confidently gives an answer. And because it asks for no more tools, the loop declares the task complete.
Nothing crashes, and that is what makes the failure dangerous.
This article is the first step toward understanding failures like that. It builds the loop twice: first by hand, then with LangChain's create_agent. Along the way, we will see what an agent loop actually is, what the framework adds, and why "the model stopped" is a weak definition of success.
Let's begin!
The agent loop and the harness
Before writing code, we need two definitions that the rest of the course leans on.
Agent
LangChain's documentation defines an agent as a model calling tools in a loop until a task is complete.
The model reads the conversation so far. It either answers or asks for a tool. If it asks for a tool, the tool runs and its result goes back into the conversation. Then the model reads again.
Harness
The second term is harness. LangChain's documentation defines a harness as everything around that loop: the prompt, the tools, and any middleware that shapes the model's behavior.
These two definitions organize this course. An agent is a loop, and a harness surrounds it.
Note on reference project:
The code and project setup are attached below as a zip file. You can extract it and run uv sync to get going.
Download the zip file below: