Tool Use & Your First Agent Loop
How does a model call tools safely and repeat the next step?
Build an agent loop with a step limit and argument validation.
📌 Learning goals
- Name the five steps: schema → call → execute → result → answer.
- Define a tool, validate its arguments, and safely run the corresponding function.
- Write an agent loop with a step limit and a stopping condition, without a framework.
- Tell function calling and structured output apart instead of treating them as the same thing.
- Compare schemas or models with fixed prompts rather than drawing a conclusion from one result.
Entry conditions
If you can run a Python file, understand functions and dicts, and have completed Stages 0–2, you are ready. If your environment is not ready, go back to Stage 0 first.
🧭 Lessons on this site
Read in the suggested order; checkboxes share the same browser progress as the /learn track pages.- 01Your First LLM API Call: Tokens, Billing, and Common Errors
Send your first LLM request with Python's openai SDK: understand the division of labor among system, user, and assistant message roles; the trade-offs of temperature, max_tokens, and streaming; token billing concepts and usage estimation; and finally, a troubleshooting guide for the errors every beginner will meet.
21 min - 02After You Press Enter: From 0.03 s to 0.85 s, What Your Question Goes Through
One real API call dissects the full chain of a question: 35 ms to establish the connection, microseconds to tokenize, 474 ms for the model to read your whole question, 380 ms generating it token by token — 854 ms in total. Includes the complete reproducible measurement method, and one counterintuitive conclusion: 96% of the time is not in the network but inside the model.
14 min - 03Function Calling 101: Getting an LLM to Actually Use Tools
A clear account of the Function Calling mechanism in mainstream LLM APIs: how to write a tool JSON Schema, how the model decides whether to call a tool, how to use tool_choice for forced calls and parallel calls — with a complete runnable example, plus practical techniques for how the way you write tool descriptions decides call quality.
26 min - 04Your First Agent: A Hello World That Uses Tools
Hand-write an Agent that actually does things, without any framework: it checks the weather, does arithmetic, and loops on its own until the task is complete. A step-by-step breakdown of the six steps of the Agent loop, its termination conditions, and the common pitfalls — once you've written it, you'll understand what frameworks do for you.
28 min - 05MCP (Model Context Protocol) Ecosystem Explained: The Three Core Primitives of Tool, Resource, and Prompt
A complete guide to Anthropic's MCP protocol: the design principles behind Tools, Resources, and Prompt Templates, practical configuration, and Claude Code integration.
10 min - 06The Ultimate Comparison of 7 AI Agent Loop Engineering Architectures: From while(true) to Multi-Agent Orchestration
A comparison of seven agent loop architectures: Claude Code, Cursor, Aider, Cline, SWE-agent, OpenHands, and our in-house Loop Engineering. Covers five dimensions: loop shape, tool execution, context management, error recovery, and verification mechanisms.
35 min - 07Three-Layer Agent Collaboration Framework: Hermes → OpenClaw → Claude Code Architecture Explained
An in-depth analysis of a three-layer AI Agent collaboration architecture built on Hermes Agent, OpenClaw, and Claude Code: the responsibilities of each layer, the communication mechanism, and why it is designed this way.
6 min
⚠️ Five guardrails before your first agent
- 1.Execute only tools in the allowlist; never use a model-generated name for arbitrary function calls.
- 2.Treat tool arguments as untrusted input; validate types, ranges, and permissions first.
- 3.Give a tool only the minimum permissions needed to complete the task.
- 4.Require human confirmation before high-risk actions such as deleting, paying, or sending email.
- 5.Set a maximum number of turns, a timeout, and a cost limit; do not let the agent loop forever.
📚 Required reading
- 1.Ollama — Tool Calling⭐⭐⭐⭐⭐Start with the single-tool and multi-turn loop.
- 2.Anthropic — How Tool Use Works⭐⭐⭐⭐⭐See what the model, application, and tool result each do.
- 3.ReAct paper⭐⭐⭐⭐Read the abstract first; learn where reasoning + acting comes from without finishing every equation at once.
🎯 Curated resources
| Resource | Who it's for | Priority | Why |
|---|---|---|---|
Official docs Anthropic — Handle Tool Calls | Everyone | ⭐⭐⭐⭐⭐ | See how call ID, result, and is_error match back to the request. |
Official docs OpenAI — Function Calling | On the OpenAI path | ⭐⭐⭐⭐ | Compare function schema and strict mode. |
Official courses & examples Anthropic Courses — Tool Use | Want to follow a notebook | ⭐⭐⭐⭐ | Work the tool-use notebooks, from a single tool to parallel tools. |
Official courses & examples Anthropic Tool Use Cookbook | After the exercises | ⭐⭐⭐⭐⭐ | See how a full app connects tools. |
Official courses & examples Anthropic Quickstarts | Want a runnable skeleton fast | ⭐⭐⭐⭐ | Maintained; MIT. |
Official courses & examples Microsoft AI Agents for Beginners | Want another complete course | ⭐⭐⭐ | Pick a single chapter. |
From-scratch implementations pguso/ai-agents-from-scratch | After Exercise 3 | ⭐⭐⭐⭐ | Compare with Exercise 3's loop using Ollama. |
From-scratch implementations mattambrogi/agent-implementation | Want to read a minimal teaching toy line by line | ⭐⭐ | Historical reference (last push 2024-01). |
Framework comparisons Hugging Face Smolagents | After the JSON-tool loop | ⭐⭐⭐⭐ | Compare CodeAct against the loop you wrote. |
Framework comparisons LangChain ReAct Agent | Want to see how a framework wraps your loop | ⭐⭐⭐ | Maintained; MIT. |
Chinese chapter-style textbooks datawhalechina/hello-agents | Want complete Chinese chapters | ⭐⭐⭐⭐⭐ | Use this route; run the code alongside it. |
Structured Output tools 567-labs/instructor | Want typed models, validation, and retry | ⭐⭐⭐⭐ | Former jxnl/instructor redirects here; MIT. |
Structured Output tools dottxt-ai/outlines | Want to study constrained decoding locally | ⭐⭐⭐⭐ | Maintained; Apache-2.0. |
🛠 Hands-on practice (upstream)
Full exercises & starter codeSummaries from the upstream curriculum; full code, cost, and latency estimates live upstream.
- Exercise 1: function calling — one tool, one call; watch the full round trip of tool call → execute → tool result (the local Ollama path costs $0 in API fees).
- Exercise 2: multi-tool selection — the model picks between calculator and get_weather; the program dispatches allowlisted names only.
- Exercise 3: ReAct from scratch — no framework; write a 13-line agent loop with a MAX_STEPS limit, starting from a mock test that needs no key.
- Exercise 4: multi-step reasoning — check Taipei's temperature, then convert it; watch how call IDs and results chain.
- Exercise 5: error handling — send recoverable tool errors back to the model; stop explicitly on transport, parsing, or limit errors.
- Exercise 6: schema design — compare a bad schema against a good one on the same fixed tasks; name which description, field, enum, or constraint helped.
- Recommended mini-project: a safe weather helper — wire up exercises 1–6 with two read-only tools, an allowlist, argument validation, MAX_STEPS, and a five-case eval.
✅ Self-check
- I can explain schema → call → execute → result → answer in my own words.
- I can distinguish tool call, tool result, and structured output.
- My program dispatches only allowlisted tools, validates arguments, and has MAX_STEPS.
- I ran Exercises 1–3 and saw at least one successful and one error path.
- When comparing models or schemas, I used the same test set and explicit scores.
Adapted from awesome-agentic-ai-zh (MIT, by Wenyu Chiou) v2026.09.23; links checked 2026-08-27. Stars mark learning priority (⭐⭐⭐⭐⭐ = you will get stuck without it), not popularity. MIT License · Curriculum structure last updated 2026-10-03. Content is still being filled in; lessons marked “in progress” are not live yet.