Most tutorials that teach you to build an AI agent start with installing a framework. This one starts with the opposite: a loop small enough to hold in your head.
Anthropic puts it bluntly — agents are "typically just LLMs using tools based on environmental feedback in a loop." If you can call an LLM API, you can build an agent from scratch. Here are the core ideas from the guide:
An agent is a loop, not a framework. The model decides what to do next, your code runs tools, results feed back in, and the cycle repeats until the task is done. A single prompt or a canned chatbot script has no feedback — so it's not an agent.
The core loop in ~10 lines of pseudocode. Define the goal and system prompt, send context, parse the model's decision, execute the tool, feed the result back, repeat — with a hard cap of 15–20 steps so an unhappy agent can never loop forever. That's the number-one guardrail for a first agent.
Tools are just JSON Schemas — and the description is the most underrated line of code. The model decides when to call a tool based on your description. Keep the toolset small (under 20 functions per turn), and design return contracts where success and failure look unmistakable. On a malformed call, return a clean
error: malformed call, please try againso the model retries.Memory: keep the window honest. Short-term memory is the transcript itself — and it eats your context window fast. Truncation and summarization delay the wall, but don't cure it: agents can still run out of context mid-task. Long-term memory lives outside the loop — files, progress logs, retrieval stores — reconstructed from artifacts each new session.
Build from scratch once, then frameworks become an informed choice. Anthropic recommends the simplest solution possible, reserving agents for open-ended problems with unpredictable step counts. Build the loop yourself until you understand every line — then trade control for velocity deliberately.
Go deeper
The full guide walks through every step — the vendor-neutral loop, OpenAI and Anthropic function-calling shapes, the write-back memory loop most agents skip, planning and task decomposition, and one runnable sketch that ties it all together:
👉 Build an AI Agent from Scratch: Loops, Tools, Guardrails
Originally published on Agentic Affairs (https://agentic-affairs.com)
Top comments (0)