An AI agent is just a loop: prompt the model, the model decides which tool to call, you execute the tool, you feed the result back, repeat until the model says it's done. That's the entire concept. Frameworks like LangGraph, CrewAI, and OpenAI's Agents SDK add convenience on top, but understanding the loop is more valuable than learning a framework. This guide walks through building a real agent in 30 minutes using the minimum viable stack — and then the patterns that matter when you want it to actually ship.
What changed in 2026
- Tool use became reliable. GPT-5, Claude Opus 4.7, and Gemini 3 Pro all hit ~95% accuracy on standard tool-use benchmarks. The "model hallucinates tool calls" problem is largely solved.
- Frameworks consolidated. LangGraph for complex workflows, OpenAI Agents SDK for OpenAI-only, plain SDK for everything else. CrewAI faded; AutoGen pivoted.
- MCP (Model Context Protocol) shipped as the standard for connecting agents to external tools and data sources.
The minimum viable agent
Here's a working agent in ~40 lines of Python using the Anthropic SDK:
from anthropic import Anthropic
import json
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):
return f"72°F and sunny in {city}" # mock; real impl calls a weather API
def run_agent(prompt, max_iterations=10):
messages = [{"role": "user", "content": prompt}]
for _ in range(max_iterations):
resp = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=tools,
messages=messages,
)
if resp.stop_reason == "end_turn":
return resp.content[0].text
# tool use
tool_use = next(b for b in resp.content if b.type == "tool_use")
result = get_weather(tool_use.input["city"])
messages.append({"role": "assistant", "content": resp.content})
messages.append({"role": "user", "content": [{
"type": "tool_result",
"tool_use_id": tool_use.id,
"content": result
}]})
return "Max iterations reached"
print(run_agent("What's the weather in Tokyo?"))
That's it. A real agent — the loop runs until the model decides it has answered, or until max iterations.
What the loop actually looks like
| Step |
What happens |
| 1 |
User prompt → model |
| 2 |
Model returns either text (done) or a tool call |
| 3 |
If tool call: execute tool, append result |
| 4 |
Loop back to step 1 with updated messages |
| 5 |
Terminate on end_turn or max iterations |
The max-iterations gate is critical — without it, a confused model can spin forever.
What separates toy agents from production agents
Tool descriptions are everything. The model's accuracy at picking the right tool is determined almost entirely by your tool descriptions and parameter schemas. Be precise: "Get the current price of a stock given the ticker symbol" beats "stock tool".
Error handling. Tools fail. The model may not know what to do. Catch exceptions, return structured error messages the model can reason about: {"error": "API rate limit", "retry_after": 30}.
Termination conditions. Max iterations, max tokens, max wall-clock time, max cost. Pick all four and enforce them.
Observability. Log every tool call, every response. You will need this when you debug. LangSmith, Braintrust, Langfuse all work; rolling your own with simple JSON logs works too.
Human-in-the-loop on destructive actions. Any tool that mutates state outside the agent (sending email, writing to DB, transferring money) should require user confirmation. Don't trust autonomous agents with the credit card.
Where agents actually work in 2026
| Use case |
Reliability |
| Research / multi-step search |
High |
| Code review / triage |
High |
| Customer support routing |
High |
| Data analysis / SQL |
Medium-high |
| Multi-tool research with synthesis |
Medium |
| Open-ended planning + execution |
Low (still) |
| Computer use / browser automation |
Low-medium |
The pattern: agents that have a clear definition of "done" succeed. Agents asked to "figure out what to do" still fail unpredictably.
When to use a framework
For a first agent — don't. The plain SDK teaches you the model better. For production complex flows (multi-agent, conditional routing, persistent state), reach for LangGraph or OpenAI Agents SDK. For RAG-heavy agents, also consider LlamaIndex.
FAQ
What model should I use?
GPT-5, Claude Opus 4.7, or Gemini 3 Pro for serious work. Smaller models (Claude Haiku, GPT-5 mini) for cost-sensitive narrow agents.
How much does an agent cost to run?
Per agent execution: $0.005 – $0.50 depending on iterations and model. Budget for it; design for cost.
Can I run agents locally?
Yes — Ollama, vLLM with Llama 4 or Qwen 3. Quality is lower than frontier but improving. Start with hosted; localize if you have specific reasons (privacy, cost at scale).
Should agents be autonomous?
Less so than the hype suggests. Most production wins are human-in-the-loop, not autonomous.
Where to go next
For related material see Building LangGraph agents in 2026, AI coding agents workflows in 2026, and MCP Claude Desktop setup in 2026.