Skip to content
Agent Builder
Module 5Intermediate

Build Your First Agent from Scratch

Write the loop yourself and truly understand it

Build a working agent in code: API setup, a minimal agent loop, real tools, streaming, error handling — then level up with an agent SDK.

Lessons
6
Reading time
75 min
Level
Intermediate
Access
Full course

Lessons in this module

  1. 5.1

    Project Setup: SDKs, Keys, and Your First API Call

    Set up a TypeScript project, store your API key safely, and make your first model call — with a Python equivalent for every step.

    10 min
  2. 5.2

    A Minimal Agent Loop in 60 Lines

    Write a complete tool-using agent loop from scratch and see exactly how messages, tool calls, and results flow.

    11 min
  3. 5.3

    Adding Real Tools: Files, Web, and APIs

    Give your agent useful abilities — reading and writing files, fetching web pages, and calling APIs — with safe implementations.

    14 min
  4. 5.4

    Streaming and a Better User Experience

    Stream tokens and tool activity to users so your agent feels fast and trustworthy, in a CLI and a web UI.

    12 min
  5. 5.5

    Error Handling, Retries, and Stop Conditions

    Make your agent robust: tool errors the model can recover from, API retries, iteration limits, and budgets.

    16 min
  6. 5.6

    Leveling Up with an Agent SDK

    When to stop hand-rolling the loop. Rebuild your agent with an agent SDK and get context management, permissions, and subagents for free.

    12 min

About this module

Underneath every agent product is a fairly small program. It sends a conversation and a list of tool definitions to a language model API, checks whether the model answered or asked to use a tool, runs the tool, appends the result to the conversation, and calls the model again. Writing that program yourself is the fastest way to understand what agents can and can't do, and why they fail the way they do.

In this part you build a real agent in TypeScript, one piece at a time, using the Anthropic API, with Python versions of the setup steps and the core loop. You start with a clean project, an API key stored where it can't leak, and a first model call, and you learn that the API has no memory: a conversation is just a list of messages you send each time.

Next you write a complete tool-using agent loop in about 60 lines and read the exact messages that pass between your code and the model. You then give the agent real tools for files, web pages, and a live API, implemented defensively and organized in a tool registry. You add streaming so users see text and tool activity as it happens, in both a terminal interface and a web page, and make the agent robust with API retries, recoverable tool errors, turn limits, token budgets, and timeouts.

Finally, you rebuild the agent on the Claude Agent SDK, which provides context management, permissions, and subagents, and compare it with the lighter-weight Tool Runner and with the OpenAI Agents SDK. You will come away knowing when an SDK saves you time and when hand-rolling the loop is still the right call.

Questions about Build Your First Agent from Scratch

How do I build an AI agent from scratch?

Write a loop that sends messages and tool definitions to a model API, runs any tool the model requests, appends the result, and repeats until the model gives a final answer. Then add real tools, error handling, and stop conditions such as turn limits. A minimal working version fits in roughly 60 lines of code.

What programming language is best for building AI agents?

TypeScript and Python are the most common choices, since major model providers publish official SDKs for both and most agent frameworks support them. Pick the language you or your team already use; the agent loop works the same way in either.

Do I need a framework to build an AI agent?

No. A basic agent needs only a model API and a loop you write yourself, and building one by hand is the best way to learn how agents work. Frameworks and agent SDKs become worthwhile when you need context management, permissions, subagents, and other features you would otherwise have to maintain yourself.

What is the Claude Agent SDK?

The Claude Agent SDK is Anthropic's library for building agents on the same engine that powers Claude Code. It provides the agent loop along with built-in tools, context management, permissions, and subagents, and it is available for TypeScript and Python.

How much does it cost to run an AI agent?

Cost depends on the model, the number of turns, and how many tokens each turn sends and receives, because the conversation is usually resent on every call. Short tasks can be cheap, while long multi-step runs cost much more. Turn limits, token budgets, and prompt caching keep spending predictable.