How to Build an AI Agent: 5 Steps and Real Commands
To build an AI agent, you put a language model in a loop, give it tools it can call, and stop the loop with limits you set. POLYROB’s own step loop has five stages (prepare, call the model, validate, run tools, record), and by default an interactive run stops after 100 steps and an autonomous run after 150 steps or $2.00 of spend.
This guide walks through the five decisions behind every agent, in the order you make them, with the commands that do each one in a self-hosted agent. The POLYROB facts come from the public repository at release v1.7.9, and the files are listed at the end.
What are the steps to build an AI agent?
- Define the job as a file or a result you can check.
- Pick the model that drives the loop.
- Give it tools.
- Give it memory.
- Set limits, and decide what waits for your approval.
Skip any one of them and you have a script that calls a model, or an agent you cannot leave alone. IBM’s guide to the topic describes the same ground as designing the agentic workflow, choosing the right LLMs and choosing the agent framework (IBM, fetched 2026-11-14). Anthropic publishes a tutorial that builds a tool-using agent on its own API (Claude Platform docs), and OpenAI has a practical guide to building agents. The steps below are model-neutral.
How does an AI agent work under the hood?
An agent is a loop. In POLYROB the core unit is the Task agent, and each step runs the same five stages (docs/guide/[email protected]):
prepare_step
-> call LLM (with tool schemas + memory context)
-> validate response (schema check, tool-call repair)
-> execute tools (via Controller)
-> record results (memory write, telemetry)
-> finalize step (check done / error / compaction)
The loop ends when the model calls done(), when a budget cap is hit, or when the session is cancelled. If you write your own, the skeleton is short:
messages = [{"role": "user", "content": task}]
for step in range(MAX_STEPS): # a cap: never loop forever
reply = call_model(messages, tools)
if not reply.tool_calls:
break # the model answered in text
for call in reply.tool_calls:
messages.append(run_tool(call))
call_model and run_tool are yours to write, and most of the work in a real agent lives inside them: retries, schema validation, cost tracking, and what to do when a tool fails.
Step 1: How do you define what the agent should do?
Name the artifact. “Research AI companies” returns prose. A request that names a file you can open returns something you can check. The examples in POLYROB’s docs use this form (docs/[email protected]):
polyrob run "read ./src and write architecture.md: modules, dependencies, data flow"
polyrob run -t browser,filesystem "find the top 3 launches on Product Hunt today and save them as markdown"
Both name an output file. When a run ends, you can read the file and decide whether the agent did the job. Pick one job like this for your first agent, and leave the general assistant for later.
Step 2: Which model should drive the agent?
The model decides which tool to call next, so it sets the ceiling on what the agent can do. Pick it by testing your own task, not by a leaderboard.
POLYROB has a native multi-provider layer with six built-in providers behind one BaseChatModel interface, and any endpoint you declare in providers.yaml with no code. It uses each provider’s structured function-call protocol, with no JSON parsing tricks (docs/guide/[email protected]). If a provider rate-limits or fails, the request backs off, then moves to the same model family on another provider you have configured. OpenAI and Anthropic are among the built-in providers (docs/guide/[email protected]).
To run a model on your own hardware, see running an LLM locally. A small local model can handle chat and still fail at agent tasks, so test it on your real job before you rely on it.
Step 3: What tools does an AI agent need?
A tool is a function the model may ask for. Start with the smallest set that does your job, because every tool you add is a new thing that can go wrong.
POLYROB groups tools into named toolsets: minimal, safe, default, research, coding, browser and others (docs/guide/[email protected]). You pick one per run:
polyrob run "scrape these pages into a CSV" --toolset research --max-steps 80
A few defaults worth knowing from the architecture doc:
web_fetchreads one URL as clean markdown without a browser, and it is the lightweight default reader.- Browser automation (Playwright) is opt-in.
- Code execution is off by default and never loaded in a default toolset.
- MCP lets you connect any compatible server, and none are configured out of the box. The MCP client guide shows how.
For work that splits cleanly, the agent can hand a piece to a sub-agent through delegate_task. Sub-agents are leaf nodes: they cannot delegate again, and they run with a least-privilege tool set (SUB_AGENTS_ENABLED is on by default). For a reusable procedure rather than a new function, see agent skills.
Step 4: How do you give an AI agent memory?
Without memory, each session starts cold and you re-explain everything. The default POLYROB backend is a local SQLite file with keyword full-text search (FTS5), and an optional vector backend adds embeddings. The flow in a session has three parts (docs/guide/[email protected]):
- At the start of each step, relevant memories are prefetched and added to the context.
- After each step, new findings are written to the store.
- The agent can also search memory itself with the
session_searchaction.
Cross-session recall is scoped by user, so one tenant’s memory is not read for another. The AI agent memory guide goes deeper on what is stored and how recall works.
Step 5: How do you keep an agent from running away?
Set the limits before the first real run. POLYROB’s defaults, from docs/[email protected]:
| Setting | Default | What it bounds |
|---|---|---|
CHAT_MAX_STEPS | 20 | A chat-surface turn |
INTERACTIVE_MAX_STEPS | 100 | A REPL turn, polyrob run, the HTTP task API |
AUTONOMOUS_MAX_STEPS | 150 | A goal, cron or delegated run |
AUTONOMOUS_RUN_BUDGET_USD | 2.0 | Spend for one autonomous run |
RUN_BUDGET_USD | 0 (off) | Session spend, interactive |
Two of these are easy to misread. The $2.00 cap applies to one autonomous run, so it is not a daily or monthly budget. And RUN_BUDGET_USD is off until you set it:
RUN_BUDGET_USD=0.50 polyrob run "Research the top 20 vector databases and compare them"
When a run reaches its cap it stops and says so. The run record names the reason (stop_reason, for example max_steps or run_budget), so it is never reported as finished when it was not.
The second half of this step is approvals. Fetched web content, mail bodies and tool output reach the model as untrusted data, not as instructions, and a correspondent the agent contacted first has its replies delivered as data too. Only the owner steers on a chat surface (docs/guide/[email protected]). The AI agent security guide covers the threat model, and the autonomous AI agent guide explains what happens when you switch the self-directed loops on.
How do you build the agent yourself, or start from an existing one?
You have two honest options.
| Write the loop yourself | Install a self-hosted agent | |
|---|---|---|
| First run | Your own code, from the skeleton up | One install command, then polyrob run |
| Step and spend caps | A counter and a cost tally you write | Step caps by default, a spend cap on autonomous runs, stop reason in the run record |
| Tools | A schema and a handler per tool | Registered tools and named toolsets |
| Memory | A store, a search, a prompt step | SQLite recall, prefetched each step |
| Chat apps and schedules | A bot per app, a scheduler | Eight chat surfaces and polyrob job new |
| What you learn | How every part works | How to operate one |
Writing it yourself teaches the most. Starting from an agent gets you to a working job sooner. If you choose the second, the install is one command on Linux, macOS or WSL2 (docs/guide/[email protected]):
curl -fsSL https://polyrob.dev/install.sh | bash
polyrob doctor # catches a missing or malformed key first
polyrob run "summarize https://example.com"
Then put the work on a clock. A scheduled job needs CRON_ENABLED on:
polyrob config set CRON_ENABLED true
polyrob job new "every day 09:00" 'Check https://example.com/product/12345 and tell me if it is under $500'
polyrob job list
See the self-hosted AI agent guide for running it on a server, or building an agent with Claude for a version of this guide that writes the loop against one provider’s API.
Where does POLYROB fall short for this?
- It has eight chat surfaces, and no native desktop or phone apps.
- You run it: you install it, update it and watch the cost. The installer supports Linux, macOS and WSL2.
- The serverless sandbox backends are tested against fakes so far.
- It is a young project with a smaller extension ecosystem, and its crypto code is an opt-in extra that has not been audited.
- Writing the loop yourself teaches more than installing one.
Questions about building an AI agent
Is it free to build an AI agent?
The software can be. POLYROB is MIT licensed, so you pay your model provider and nothing for the agent, or nothing at all with a model you host. The part that costs money is the model calls, which is why the spend caps in step 5 matter. See the open source AI agent guide for what the license covers.
Can ChatGPT build an AI agent?
ChatGPT is a chat product. To build an agent you call a model through an API inside your own loop, or use an agent that does. OpenAI’s models work as the model behind POLYROB, since OpenAI is one of its built-in providers. The agent, with its tools, memory and limits, is what you run.
Is it easy to build an AI agent?
A first loop is short to write and hard to make dependable. The skeleton above is eight lines. What takes the time is retries, tool validation, cost tracking and deciding what needs your approval, so starting from an installed agent and writing only the job is a reasonable choice.
Where do you start?
Pick one job that ends in a file, run it with --max-steps and a small RUN_BUDGET_USD, and read what the run record says. Install POLYROB or read the docs. The source is MIT licensed at github.com/theselfruleorg/polyrob.
Sources
POLYROB facts are from the public repository at release v1.7.9 (github.com/theselfruleorg/polyrob).
- docs/guide/[email protected] (step loop and stages, provider layer and failover, tools, memory flow, sub-agents, correspondent replies as data)
- docs/[email protected] (
CHAT_MAX_STEPS20,INTERACTIVE_MAX_STEPS100,AUTONOMOUS_MAX_STEPS150,AUTONOMOUS_RUN_BUDGET_USD2.0,RUN_BUDGET_USD0,SUB_AGENTS_ENABLED) - docs/guide/[email protected] (
polyrob run,--toolset,--max-steps, toolset names) - docs/[email protected] (named-artifact examples,
polyrob job new,RUN_BUDGET_USD, stop reason) - docs/guide/[email protected] (section 1: fetched content, mail and tool output are untrusted data)
- docs/guide/[email protected] (section 4:
anthropic,openai,gemini,nvidia,deepseekproviders) - docs/guide/[email protected] (installer, supported platforms,
polyrob doctor) - IBM, How to Build an AI Agent, fetched 2026-11-14 (page description).
- Anthropic, Tutorial: Build a tool-using agent, fetched 2026-11-14 (page title).
- OpenAI, A practical guide to building agents (title as shown in the search results on 2026-11-14; the page itself was not readable from the server).
POLYROB is an open-source AI agent you run and own. Install it or read the docs.