Table of contents #
Over the past year, I’ve written a handful of agents. Some of them handle production traffic at work, and a couple run on a Raspberry Pi on my desk. I’ve written agent loops by hand and used most of the popular frameworks: AWS Strands, Google ADK, LangGraph, OpenAI Agents SDK, Pydantic AI, and the Claude Agent SDK. Along the way, I settled on a pattern that I now use for nearly every agent, regardless of the framework.
Note
By agent, I mean a model plus a harness. The harness is the glue code around the model that does things on its behalf, like calling tools, reading files, and talking to other services.
Agents can be triggered by an API call, an event, or a cron job. Once awake, the agent typically follows the same steps, and each run is one pass of a reconciliation loop.
Reconciliation loops #
In Reconciling Everything, Andrew Godwin describes a reconciliation loop as stateless programs that talk to one source of truth. Each program reads the current state and finds something that doesn’t match the desired state. It takes a small step to close the gap, writes the result back, and starts again.
Kubernetes is the best-known example. You declare what you want in an object’s spec, and a
controller runs a loop that:
- watches the API server for changes to the objects it manages
- compares the desired state in the
specwith the current state of the cluster - asks the API server for the changes that close the gap
- records what it saw in the object’s
status
Observe, decide, apply, report #
An agent run works the same way, with your prompt or rules defining the desired state.
In code, a run looks like this:
def reconcile(trigger: Trigger) -> None:
state = observe(trigger)
plan = decide(state) # returns data, changes nothing
result = apply(plan)
report(result)
Say you’re building an agent that labels and assigns open GitHub issues:
observefetches the currentstate, like the open issues that still need triage. Usually this is plain code calling an API. If fetching the data directly is cumbersome or finding the right context takes judgment, it can ask a model to use read-only tools instead.decidecomparesstateagainst your prompt or rules and returns aplanas plain data, like which labels and owners each issue should get. You can write it in ordinary code, or call an LLM or a decision model.applycarries outplanand makes the changes on GitHub. Depending on the job, it runs the steps directly or hands them to subagents or a swarm.reportturnsresultinto a summary and handles notifications, like posting what changed to Slack.
In a typical tool-calling loop, the model reads state and mutates it in the same turn. You
can’t see what it’s about to do before it acts. Splitting decide from apply gets you
closer to functional core, imperative shell. Calling a model over the network means
decide isn’t strictly pure, since you still have I/O and non-determinism. Still, it never
modifies the system you’re managing, and every change happens in apply. Because the plan
is plain data, I can log it before anything runs or skip apply for a dry run. I can also
replay recorded state through decide in tests.
One call to reconcile is a single pass, which is enough for an agent behind an API
endpoint. Put the same function on a schedule and it becomes a loop. As long as observe
sees the effect of apply on the next run, the agent doesn’t need its own state or
checkpointing. Say that GitHub triage agent crashes halfway through a run. GitHub holds the
state, so the next run just finds whichever issues are still unlabeled and finishes the job.
If you want to see how this looks in real code, I have put together a single-pass example in this gist. It’s an agent written with the AWS Strands SDK that finds the most controversial stories on Hacker News and summarizes them.
Outside of software, I’m also building an agent that moves the arms of a servo robot, and this loop fits each individual movement perfectly. Maybe I’ll write about it at some point.