---
title: Implement an agent loop in Python that takes a goal, a set of tools with JSON-schema arguments and a model client…
url: https://gurkanozkan.ai/learn/questions/applied-llm-agt-03/
markdown: https://gurkanozkan.ai/learn/questions/applied-llm-agt-03.md
kind: question
role: Applied LLM
stage: Coding
difficulty: Medium
json: https://gurkanozkan.ai/api/learn/questions/applied-llm-agt-03.json
robots: noindex
---

# Implement an agent loop in Python that takes a goal, a set of tools with JSON-schema arguments and a model client. Handle tool errors, a step limit and a token budget; then let independent tool calls run concurrently.

Applied LLM · Coding · Medium

Theme reported publicly: [Cohere](https://builtin.com/job/forward-deployed-engineer-agentic-platform-uk-europe/9328104)

## Answer

*Model answer, written in a candidate's voice*

### In short

- Model call -> validate JSON-schema args -> execute -> append results with matching call IDs -> repeat until final answer or limit
- Tool/validation errors become informative tool results; transient API errors retry with exponential backoff and jitter
- Budgets: max steps, token/cost cap, per-tool timeout, output truncation/summarisation
- asyncio.gather for independent calls in one assistant turn, preserving call IDs and isolating failures
- Structured tracing per step and tests using a scripted fake model

### In full

I would make the loop explicit: send the goal and tool schemas to the model, parse the assistant response, validate each requested tool call against its JSON schema before execution, run the calls, append tool messages whose call_id matches the request, and repeat until the model returns a final answer or a budget is exhausted. Tool failures, including schema validation failures, are returned to the model as structured results such as {error: ..., retryable: ...} rather than raised, while transient model/API errors are retried with exponential backoff and jitter and then surfaced if the retry budget is spent. I would track steps, tokens/cost from usage metadata, per-tool timeouts with asyncio.wait_for, and truncate or summarise large outputs before they enter the context. For one assistant turn with multiple independent tool calls, I would run them concurrently with asyncio.gather, keep each result tied to its call_id, and let one tool failure not cancel the others unless the budget or a hard invariant is broken. I would emit a trace per step with model latency, token usage, tool names, validation outcomes, durations, and errors, and I would test with a scripted fake model that returns queued responses to assert loop termination, ID matching, error recovery, budget stops, timeouts, and concurrency.

*Checked against sources by an independent AI reviewer.*

## What a strong answer covers

- Loop: call model → validate tool arguments → execute → append results with matching call IDs → repeat until final answer or limit
- Tool errors go back to the model as informative results instead of crashing; transient API errors retried with backoff
- Budgets for steps, tokens/cost and per-tool timeouts; large outputs truncated or summarised
- Concurrency with asyncio, preserving call IDs
- Tracing per step; tests with a scripted fake model

## Common mistakes

- No step limit
- swallowing tool exceptions so the model assumes success

## Topic guides

- [Agents and tool use](https://gurkanozkan.ai/learn/topics/agents.md)
- [Coding for ML and LLM engineering](https://gurkanozkan.ai/learn/topics/coding-engineering.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
