Back to the blog

Engineering

How to build AI agents that work in production

The engineering decisions behind a reliable agent: when to use a workflow instead, and how to design tools, manage context, test behavior and control cost.

· 6 min read

An AI agent connected to four tools, above a panel showing its run activity

A demo agent needs a prompt and a few tools. A production agent has to cope with messy input, survive failed API calls, stay within budget and explain what it did.

This guide covers the decisions that make the difference. It’s written for engineers and technical leads, and it doesn’t assume a particular model or framework.

Start with the simplest thing that can work

Not every AI feature needs an agent. Anthropic’s Building effective agents sets out a useful ladder of options, and it’s worth stopping at the lowest rung that does the job.

Pattern How it works Use it when
Single model call One prompt, with retrieved context if needed You need an answer or a draft
Prompt chaining Each step’s output feeds the next step The task splits into fixed steps
Routing A classifier sends each input down a specialized path Different kinds of input need different handling
Parallelization Several calls run at once and the results are combined Subtasks are independent, or you want several attempts to compare
Orchestrator-workers A lead model breaks down the work and delegates it You can’t know the subtasks in advance
Evaluator-optimizer One model produces work and another critiques it You have clear criteria, and iterating improves the result
Agent The model chooses tools in a loop until it’s done You can’t predict the number of steps

Most production systems combine these. A router might send routine requests down a fixed workflow and only pass the hard ones to an agent.

Keep anything deterministic in ordinary code. Don’t ask a model to add up an invoice, check a permission or decide whether a date has passed.

Design tools for the model to use

The model only knows your systems through the tools you give it, so tool design has a large effect on reliability. Anthropic’s guide recommends putting real effort into this interface, through “thorough tool documentation and testing”.

  • Make tools specific. get_order_status is easier to use correctly, and safer, than a general run_sql.
  • Describe them for the model. Say what each tool is for and when not to use it, and give examples of its inputs.
  • Return only what’s needed. Trim and paginate large results, because everything returned takes up room in the context.
  • Write useful errors. “Order not found, check the ID format” gives the model a way to recover. A bare 500 doesn’t.
  • Validate every argument in code. Treat model output as you would input from an untrusted user.
  • Make writes safe to repeat. Use an idempotency key, so a retry can’t refund a customer twice.

If several applications need the same tools, the Model Context Protocol offers a standard way to expose them, so you don’t have to build a separate integration for each one.

Manage context deliberately

The model can only reason about what’s in its context. As a long run fills up with old tool results, it costs more and can lose track of what matters.

  • Keep instructions short and specific. Vague or conflicting guidance leads to unpredictable behavior.
  • Retrieve information when it’s needed. Don’t load everything up front just in case.
  • Summarize or drop old results. A long run rarely needs every earlier tool output word for word.
  • Keep state outside the model. Task status, record IDs and decisions belong in a proper data store, not only in the conversation.

Treat each run as a long-running job

An agent run can take minutes and dozens of calls, so something will eventually fail partway through. Plan for it as you would for any background job.

  • Save state after each step, so a run can resume instead of starting over.
  • Set hard limits on steps, time and spend per run. When it reaches one, the agent stops and hands over.
  • Handle temporary failures in code. Retry timeouts and rate limits with backoff, without spending model calls on them.
  • Define clear ways to finish. It can complete the task, ask for help, or give up and explain why.
  • Hand over with context. When a person takes over, they should see what was tried and what was found.

Test behavior, not just code

Unit tests still matter for your tools, but they won’t tell you whether the agent makes good decisions. For that you need an evaluation set: real cases with known good outcomes, including edge cases and past failures.

  • Check what you can with code. Did it call the right tool, with valid arguments, and avoid anything forbidden?
  • Use model grading or human review for the rest, such as the quality of a written reply.
  • Run each case several times. The same input can take different paths, so measure a pass rate instead of a single pass or fail.
  • Run the set on every change, including prompts, tools and model versions. A small prompt edit can change behavior in unexpected places.
  • Add every production failure as a new test, so the same mistake gets caught next time.

Make every run observable

When an agent does something odd, you need to see exactly what happened. Anthropic’s guide makes transparency a core principle, recommending that you show “the agent’s planning steps”.

For each run, record the input, every model and tool call with its arguments and result, the tokens used, the time taken and the final outcome. Redact sensitive data before it reaches your logs.

Then set alerts for the signs of trouble: repeated identical tool calls, sudden jumps in spend, rising tool errors and unusual escalation rates.

Control the cost

Costs can grow quickly, because one task can involve many model calls.

  • Match the model to the step. Use a smaller, cheaper model for simple steps, and a more capable one for hard reasoning.
  • Keep context lean, since every call pays for everything in it.
  • Use prompt caching for long, stable instructions, if your provider supports it.
  • Set budgets per run and per day, and alert before you reach them.
  • Track cost per completed task. Cost per call can look fine while a task quietly takes twenty attempts.

A production-readiness checklist

Before an agent goes live, check that you have:

  • A simpler pattern ruled out, for a reason you can explain
  • Specific, documented tools with validated inputs
  • Writes that are safe to repeat
  • Limits on steps, time and spend
  • State that survives a failure partway through
  • An evaluation set that runs on every change
  • A full record of every run, with sensitive data redacted
  • A clear handover to a person
  • A way to switch the agent off quickly

Reliability is only half of production readiness. The other half is limiting what can go wrong, which we cover in how to keep AI agents safe.

More from the blog

Guide

What is agentic AI? A plain-English guide

What AI agents are, how they differ from chatbots and automation, what they’re made of, and how to tell whether your business needs one.

· 5 min read

Read the article

Security

How to keep AI agents safe

An agent that can act can also do damage. How prompt injection and excessive access create risk, and the permissions, approvals and monitoring that limit it.

· 6 min read

Read the article

Let’s talk

Have an AI use case in mind?

Tell us what you’d like it to do. We’ll help you work out what to build first, and whether it’s worth building.