ModelRefs / How to Build AI Agents

How to Build AI Agents

Step-by-step guide to building production AI agents.

What this reference supports

How to Build AI Agents: This learning reference introduces the concept, explains how it connects to AI implementation decisions, and points to deeper profiles, workflows, benchmarks, and guides.

How to Build AI Agents: Focus on the boundary of the concept as well as its benefits. Understanding what a method cannot establish matters when interpreting model claims, benchmark results, provider features, or workflow designs.

How to Build AI Agents: Continue into the related references and apply the concept to a concrete decision with explicit constraints, evidence requirements, risks, and evaluation criteria.

Continue your research

Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to How to Build AI Agents.

Article

What is an AI agent?

An AI agent is a program that uses an LLM to decide what to do next, executes that action, observes the result, and loops until the task is done. Three properties distinguish it from a chatbot:

- Autonomy — it picks the next step without a human turn. - Tools — it can act on the world (HTTP calls, code execution, DB queries). - Persistence — it carries state across steps via memory.

The 2026 agent stack

- Model: Claude Sonnet 4.5 (best tool calling), GPT-5.5 (best planning), DeepSeek R2 (cheapest), Llama 3.3 (self-host). - Framework: LangGraph, OpenAI Agents SDK, Mastra, pydantic-ai, or roll-your-own. - Memory: Postgres + pgvector, or a managed store (Mem0, Zep). - Observability: LangSmith, Langfuse, Helicone, or Braintrust. - Sandboxing: E2B, Modal, or Daytona for arbitrary code execution.

Step 1: One tool call

Start with the simplest possible agent: an LLM that can call a single function.

```python from anthropic import Anthropic

client = Anthropic()

tools = [{ "name": "get_weather", "description": "Get current weather for a city.", "input_schema": { "type": "object", "properties": { "city": { "type": "string" } }, "required": ["city"] } }]

def get_weather(city: str) -> str: return f"{city}: 18C, partly cloudy"

resp = client.messages.create( model="claude-sonnet-4-5", max_tokens=1024, tools=tools, messages=[{"role": "user", "content": "What’s the weather in Paris?"}], )

for block in resp.content: if block.type == "tool_use" and block.name == "get_weather": print(get_weather(**block.input)) ```

That’s the seed. Everything below is a generalization of this loop.

Step 2: The agent loop

Agents work by looping: call model, execute tool, append result, call model again. Exit when the model returns no tool calls.

```python messages = [{"role": "user", "content": user_prompt}] for step in range(MAX_STEPS): resp = client.messages.create( model="claude-sonnet-4-5", tools=tools, messages=messages, max_tokens=2048, ) messages.append({"role": "assistant", "content": resp.content})

tool_uses = [b for b in resp.content if b.type == "tool_use"] if not tool_uses: break # model returned final answer

results = [] for use in tool_uses: output = TOOLS[use.name](**use.input) results.append({ "type": "tool_result", "tool_use_id": use.id, "content": str(output), }) messages.append({"role": "user", "content": results}) ```

That’s a complete agent in 20 lines. Cap MAX_STEPS at 10 to start.

Step 3: Multiple tools

Good agent design is mostly good tool design. Three rules:

1. One tool, one job. A bloated do_everything tool confuses the model. 2. Type the inputs. JSON schemas with descriptions outperform docstrings. 3. Return structured errors. "User not found (id=42)" is recoverable; raising an exception kills the run.

Typical starter toolkit: search_web, read_url, read_file, write_file, execute_python, send_email, finish.

Step 4: Memory

Real agents need three memory tiers:

- Working memory — the current run’s messages array. - Episodic memory — past runs, stored in Postgres. "What did I do for this user last week?" - Semantic memory — facts about users, projects, or domains, stored as embeddings in pgvector or a managed store.

Don’t over-engineer. Start with a single memories table: id, user_id, content, embedding, created_at. Add a retrieval tool the agent can call. That’s 80% of what production agents do.

Step 5: Graphs & planning

Once you have 5+ tools and conditional flows ("if the user is on the Pro plan, also send a Slack notification"), the linear loop creaks. Move to a graph framework.

LangGraph is the 2026 default. You define nodes (tool runs, LLM calls), edges (transitions), and a state object. The graph compiles to a debuggable, retryable, persistent workflow.

```python from langgraph.graph import StateGraph, END

graph = StateGraph(AgentState) graph.add_node("plan", plan_step) graph.add_node("execute", execute_step) graph.add_node("reflect", reflect_step) graph.add_edge("plan", "execute") graph.add_conditional_edges("execute", should_reflect, { "yes": "reflect", "no": END, }) graph.add_edge("reflect", "plan") graph.set_entry_point("plan") app = graph.compile(checkpointer=postgres_checkpointer) ```

Step 6: Human-in-the-loop

Any agent that can take destructive actions (send email, charge cards, delete files) needs a human checkpoint. Pattern:

- The agent proposes the action and pauses. - A human approves, rejects, or edits via Slack, email, or UI. - The agent resumes from the same step (use a checkpointer).

LangGraph’s interrupt primitive and the OpenAI Agents SDK’s approval hooks both handle this cleanly.

Evaluation

Most agents fail in production not because the framework is wrong but because nobody measured. Before shipping:

1. Write 20-50 representative tasks with expected outputs. 2. Run them on every model + prompt change. 3. Score: correctness, steps used, total cost, latency, any safety violations. 4. Block deploys that regress the score below a threshold.

Tools: LangSmith, Braintrust, Langfuse, or a homegrown pytest harness. Even a Google Sheet beats no evals.

Shipping to production

- Idempotency keys on every external tool call — agents retry. - Per-tool rate limits — a buggy plan can hammer an API. - Per-run cost budget — kill any run that exceeds a dollar threshold. - Structured logs — log every prompt, tool call, and tool result with a run_id. You will need it. - Versioned prompts — treat the system prompt like code: PR, review, deploy.

Common pitfalls

1. Too many tools. Models get worse past ~15 tools. Group with sub-agents. 2. Vague tool descriptions. The model picks tools based on the description, not the name. Write them like good API docs. 3. Unbounded loops. Always cap steps and detect repeats. 4. No evals. Without a test set, you’re flying blind. 5. Wrong model. Don’t use a reasoning model for trivial extraction.

What to build next

Now that you have the loop, pick a target: a research agent that compiles competitive briefs, a coding agent that fixes flaky tests, or a support agent that triages tickets. Start narrow. Ship to one user. Iterate. When you’re ready to take agents from prototype to production, pair this guide with the best AI tools in 2026 for the supporting stack, and best open-source LLMs when the API bill gets scary.

Frequently asked questions

What is the difference between an AI agent and a chatbot?

A chatbot generates a reply. An agent decides what action to take next, executes it (call an API, write a file, query a database), observes the result, and decides again. Agents have a loop; chatbots have a turn.

Which framework should I use to build an AI agent in 2026?

LangGraph (Python/JS) for production graph-based agents, OpenAI Agents SDK if you’re all-in on OpenAI, Mastra for TypeScript-first teams, and pydantic-ai for type-safe Python. For the simplest case, no framework — a 30-line loop is enough.

How much does it cost to run an AI agent?

A typical multi-step agent run costs $0.05–$0.50 in 2026 on frontier models (GPT-5.5, Claude Sonnet 4.5) or $0.01–$0.05 on DeepSeek R2. Cost scales with tool calls and context length. Route easy steps to cheaper models to cut bills 5–10x.

How do I keep an AI agent from looping forever?

Always cap max steps (start at 10), set per-tool timeouts, detect repeated tool calls, and add a ‘final answer’ tool the model must use to exit. Frameworks like LangGraph do this for you.

How do I evaluate an AI agent?

Build a dataset of 20–50 representative tasks with expected outcomes. Score each run on: did it produce the right final answer, how many steps, total cost, and any safety violations. Re-run after every prompt or model change. This is the single highest-ROI hour you’ll spend.