How I build an agent
My approach, from the task to the rollout. The code is a sketch of the loop.
- Pick the task and its numberWatch someone do it, record real examples, and write down what a good result is.
- Start with a fixed workflowUse code for known steps. Let the model choose steps only where the path changes by case.
- Build the contextRetrieve by permission, rank, trim to a budget, and keep the source of every fact.
- Design the toolsSmall typed actions with timeouts, least access and idempotency keys. MCP when several agents share them.
- Add approval by riskReads run alone. Sending, paying, publishing or deleting waits for a person.
- Evaluate, then releaseReplay saved cases on every change, try it in shadow, then a small share of users with rollback.
def run(task, model, tools, budget):
state = load_checkpoint(task.id) or State(task)
while state.steps < budget.steps and state.cost < budget.usd:
context = retrieve(task, state, user=task.user) # only what this user may read
action = model.next(state, context, tools.schemas)
if action.done:
problems = verify(action.result, context)
if not problems:
return action.result
state = state.add_feedback(problems)
else:
tool = tools[action.name]
args = tool.schema.validate(action.args)
if tool.writes and not approved(task, tool, args):
return pause_for_approval(state, action)
key = f"{task.id}:{state.steps}" # a retry cannot write twice
state = state.add(action, tool.run(args, key, tool.timeout))
save_checkpoint(state)
trace(state)
return hand_to_person(state, "budget reached")
When is an agent the wrong choice?
When the steps are known and the cost of a mistake is high. A workflow with one or two model calls is cheaper, faster and easier to test.
RAG, long context or fine-tuning?
RAG when facts change or need permissions and citations. Long context when one bounded document set fits. Fine-tuning for stable behavior or format, after prompts and evals stop improving.
How do you evaluate it?
Three levels: each tool call, the final result, and the business number. Code checks come first. I use a model as judge only after comparing it with human labels on the same cases.
One agent or several?
One until a measured limit appears. I split when tasks need different permissions, separate context or parallel work, because each handoff can lose information.
How do you handle prompt injection?
Treat retrieved text and tool output as data. Keep permissions outside the model, limit where data can go, and require approval for risky writes. A system prompt alone cannot enforce this.
How do you control cost and delay?
Budgets per task, prompt caching, a smaller model for simple steps, parallel reads, and early exit. I track cost for each successful task, since a cheap failure still costs.
What happens when a tool fails?
Retry only what is safe, with backoff and the same key. Then return a clear error the model can act on, try another route, or hand over with the full trace.
What do you keep in memory?
Task state in a checkpoint, durable facts in a store with a source and date, and the rest out. Users must be able to see and correct what is kept.