Courses / Agentic AI / Module 2
Module 2: Tool design: schemas, errors, idempotency, rate limits (Python and TypeScript labs)
10 h · theory 3 · hands-on 4 · project 2 · revision 1
You will be able to
- Write tool schemas a model uses correctly
- Return errors the model can recover from
- Make side-effecting tools idempotent
- Build the same tool in Python and TypeScript
Capstone increment: Tool library for the ops assistant (logs, metrics, tickets) with typed schemas, retries and tests.
Job-description skills: Tool design, TypeScript · Lab tools: pydantic, node, zod
1. Tools are an interface for a model
A tool’s name, description and parameter docs are a prompt the model reads every time. Thin wrappers over every API endpoint make agents slow and confused; a few tools designed around the tasks the agent actually does work better. Anthropic’s engineering guide on writing tools for agents makes the same point: consolidate, name clearly, and return what the model needs, not everything the API has.
Between the model’s request and the real system sit validation, authorisation, execution with timeouts and idempotency, and result shaping. Each layer has a job.
2. Schemas the model uses correctly
Use enums for closed sets, explicit units and formats (“window_minutes”, “ISO 8601 date”), sensible defaults and bounds. Put one short example in the description when the format is unusual. Namespace related tools (logs_search, logs_tail) so the model can tell them apart, and avoid two tools that do nearly the same thing.
Shape results for the model: a few meaningful fields, human-readable names instead of bare ids, and a limit with a clear note when results are truncated (“42 more lines; narrow the query”). Offer a response-format option (concise or detailed) when callers need both.
3. Errors the model can recover from
A stack trace is useless to a model; “unknown service ‘paymnts’. Did you mean ‘payments’ or ‘payments-worker’? Call list_services to see all names.” fixes the next call. Say what went wrong, how to fix it, and whether retrying can help. Never put secrets or internal details in error text: it goes into the model’s context and into traces.
Measure recovery: replay calls with deliberately bad arguments and count how often the model fixes them on the next turn. Rewriting error messages is often the cheapest reliability win an agent gets.
4. Idempotency, retries and rate limits
Networks fail in the worst way: the server did the work and the response was lost. If the agent retries a create_ticket call, you get two tickets. Pass an idempotency key derived from the run and step, so the server returns the existing result for a repeat. Retry reads freely with backoff; retry writes only with keys.
Rate-limit each tool with a token bucket, so a looping agent can’t open a hundred tickets in a minute, and add timeouts everywhere. Try the simulator to see retries, duplicates and keys interact.
Tickets created 188 (200 wanted) · duplicates 0 · calls the agent saw fail 21 · of those, tickets that exist anyway 9 · network attempts 200
Guided experiments
- No retries. How many calls fail, and how many of those “failures” actually created a ticket? What will the agent do next if you return a plain timeout error?
- Allow 3 retries. Failures vanish, but count the duplicates. Who notices duplicate tickets, and what does it cost?
- Turn on the idempotency key. Why does the same retry policy now give exactly 200 tickets? Where does the key come from: the model, or your tool code?
- Raise both loss rates to 20%. Which tools in an ops assistant are safe to retry blindly (reads) and which need keys (writes, payments, messages)?
5. The same tool in Python and TypeScript
Teams often run agents in Python and product code in TypeScript. Keep one contract: the JSON Schema. In Python it comes from Pydantic; in TypeScript from Zod (z.toJSONSchema in Zod 4). Store example calls and expected results as JSON fixtures and run the same contract tests against both implementations.
When the two drift (a renamed field, a new enum value), the contract tests fail before a model sees the difference.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 2.1: Ops tool library
Write logs_search, metrics_query and tickets_create for the capstone with Pydantic schemas, helpful errors, limits, timeouts and idempotency keys on writes, plus pytest tests for good and bad calls.
Expected: src/tools/ with tests covering every error path.
Lab 2.2: TypeScript twins
Implement metrics_query and tickets_create in TypeScript with Zod, export their JSON Schemas, and run shared JSON fixtures against both languages.
Expected: Contract tests pass in both languages; a renamed field fails both.
Lab 2.3: Error recovery eval (capstone increment)
Replay 20 tool calls with bad arguments through a real model, before and after improving the error messages, and count fixes on the next turn.
Expected: Recovery rate before and after, with the three error rewrites that helped most.
Go deeper
Videos
- LLM Function Calling - AI Tools Deep Dive · Adam Lucek · ~31 min. A deep dive on function calling: schemas, parallel calls and building tools models use well.
- Idempotency - What it is and How to Implement it · Alex Hyett · ~8 min. Idempotency explained with the patterns you need for safe retries.
Further reading
- Article Writing effective tools for AI agents · Anthropic. Naming, descriptions, response size and error messages, tested with evals.
- Docs Idempotent requests · Stripe API reference. The best-known production design for idempotency keys, from a payments company.
- Docs The Idempotency-Key HTTP Header Field · IETF HTTPAPI working group. The draft standard for the header, including what to return for a repeated key.
- Docs Zod · Zod. Runtime validation for the TypeScript version of your tools.
GitHub repositories
- anthropics/claude-cookbooks: The tool-use section: tool definitions, error handling and returning results.
- colinhacks/zod: z.toJSONSchema() in Zod 4 and refinements for bounds and formats.
- openai/openai-agents-python: function_tool: how a Python function’s signature and docstring become a tool schema.
Checkpoint quiz
Interview questions
- How do you design tools that an LLM uses correctly?
- An agent created duplicate tickets after a network blip. Explain and fix.
- What should a tool return on error? Give an example.
- How do you rate-limit and budget tool use for a runaway agent?
- How would you keep the same tool consistent across Python and TypeScript services?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).