Skip to main content
24 min read

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.

👉
Prerequisites: Readers should be comfortable with Python programming (loops, common data structures, type hints, Pydantic basics, and writing tests). Additionally, this series assumes a clear understanding of LLM APIs, tool calling, and a technical familiarity with what an agent is. We do not re-explain those. If you'd like to brush up on any of the agent concepts, we recommend revisiting the earlier AI Agents Course, particularly Parts 1, 2, and 10. Everything else is taught inline.

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.

👉
An important distinction: Workflows versus agents. Workflows are systems where model calls and tools follow predefined code paths. Agents, on the other hand, are systems where the model directs its own process and tool use. Anthropic categorizes both of them as agentic systems.

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:

Published on Sep 13, 2026