Skip to content

AI agents

Chapter 16 0 code blocks 903 words

AI agents

Say this

An agent is an LLM in a loop with tools. It gets a goal, decides the next action, the application runs that tool and returns the result, and the model decides again until the goal is done or a limit is hit. The difference from a workflow is who decides the steps: in a workflow my code fixes the order; in an agent the model chooses. I use the simplest one that works, because every extra step the model decides is another place to fail, cost and wait. The parts are the model, the tools, memory, a stop rule, guardrails, and tracing so I can see every step.

The agent loop
  1. Goal from the user
  2. 1
    Model: think, choose the next action
    tool_call {"name": "search_products", "arguments": {...}}
  3. 2
    App: validate, check permission, run
    side effects (pay, send, create) → a human confirms first
  4. 3
    Observation back into the context
    only the fields the model needs
  5. 4
    Done?
    Yes → answer
    No → back to step 1
    until max steps, budget or timeout
1

Chatbot, workflow, agent

What
Three levels of control: a chatbot answers once; a workflow runs fixed steps written in code; an agent lets the model choose the steps.
How
Workflow patterns: prompt chaining, routing, parallel calls, orchestrator and workers, evaluator and optimizer. An agent is the open loop.
Why
Workflows are predictable and cheap; agents handle open tasks where the steps are not known in advance. Choose the agent only when the task needs it.
2

Tools

What
Functions the model may call, each with a name, a description and a JSON schema.
How
The model returns a tool call; my code validates, authorizes and runs it, then returns the result with the same call id (chapter 14).
Why
The tools are the agent's real power and its real risk. Few, clear, well-described tools beat many overlapping ones.
3

Memory

What
Short-term memory is the conversation and tool results in the context; long-term memory is stored outside and retrieved when needed.
How
Short-term: keep the history, summarize old turns when the context gets long. Long-term: a database or RAG over past notes, user facts or documents.
Weakness
The context fills up fast with tool results. Return only the fields the model needs.
4

Planning

What
How the model decides the next step.
How
ReAct: think, act, observe, repeat. Plan-and-execute: write a plan first, then run the steps and re-plan if something fails. Reflection: a second pass checks the result.
Why
A plan makes long tasks more reliable and easier to inspect in the trace.
5

Stop rules and guardrails

What
Limits that keep a loop safe and affordable.
How
Maximum steps, token or money budget, timeout per tool, least-privilege tools, and human approval before anything with a side effect.
Why
Errors compound: if each step is right 95% of the time, ten steps in a row are right only 0.9510≈0.60 of the time.
6

Evaluation and tracing

What
Measuring whether the agent finished the task, and seeing how.
How
A test set of tasks with a checkable end state; metrics: success rate, number of steps, cost, latency, wrong tool calls. Every run logs each thought, tool call, argument and result.
Why
Without the trace you cannot tell a bad prompt from a bad tool from a bad model.
PatternWho decides the stepsUse it when
Single callnobody, one answerthe task fits in one prompt
Workflow (chain, router)my codethe steps are known in advance
Single agent with toolsthe model, in a loopthe steps depend on what it finds
Multi-agent (orchestrator and workers)a lead model hands sub-tasks to othersbig tasks that split into independent parts; costs much more

Example project

  • ZargarShop is an agent: during the call Gemini decides when to search the catalog and when to create the order, depending on what the customer says. The catalog tools only read, the order tool can only insert, and staff review every order.
  • Tez is deliberately not an agent: the model writes one JSON plan, and my code executes it in DuckDB. A fixed workflow is more reliable for numbers, and there is no loop to run away.

If they push

  • MCP: Model Context Protocol, a standard way for a server to offer tools and data to any compatible AI app, so a tool is written once and reused.
  • Prompt injection through tools: a web page or document can contain instructions. Treat tool results as data, keep tools least-privilege, and confirm side effects with the user.
  • Frameworks: LangGraph, CrewAI, OpenAI Agents SDK, Claude Agent SDK. A plain loop is enough for small agents and easier to debug.
  • Cost and latency: each step is a model call; cache the system prompt, use a smaller model for easy steps, and stop early.

Trap questions

  • "Is every LLM with tools an agent?" No. One tool call in a fixed flow is a workflow; an agent loops and chooses.
  • "Are more agents better?" Usually not. More agents means more cost, more hand-offs and more places to fail. Start with one.
  • "How do you stop an infinite loop?" A hard maximum on steps and budget, detecting repeated identical calls, and a fallback answer.

Be honest

"My production agent is ZargarShop's phone agent. I haven't run a long multi-agent system in production; for numbers I chose a fixed workflow in Tez on purpose."

Questions about this chapter?

The course, homework review and the other five books are in Uzbek. Questions in English are welcome on Telegram.

Write on Telegram Contents