Building agents with tool use

By Ryze AI Team

An agent is an LLM that can call tools, which are ordinary functions you expose to it, to take actions beyond generating text. Tool use turns a chatbot into something that can look things up, compute, query databases, and act on the world. It is the most powerful pattern in applied AI and also the easiest to get wrong.

The loop

The agent loop has four steps. The user sends a message. The model decides whether to call a tool and, if so, with what arguments. You execute the tool and return the result as a tool message. The model then either calls another tool or produces a final answer. The loop repeats until the model stops calling tools. Your code controls the loop: you decide how many iterations to allow, how to handle errors, and when to force a final answer.

Tool design

Tools should be narrow, well-named, and have clear parameter schemas. "search_docs(query)" is better than "do_stuff". Each tool should do one thing and return structured, concise results; a tool that returns a ten-thousand-token dump wastes context and confuses the model. Describe parameters precisely, including types, enums, and required fields, because the model uses the schema to construct valid calls. Handle errors gracefully and return a message the model can act on, such as "no results found" rather than a stack trace.

Safety

Separate read-only tools from mutating ones. Read-only tools such as search and lookup can run automatically. Mutating tools such as sending email, deleting files, or making purchases require user confirmation before execution. Limit the number of tools to under twenty; beyond that, models struggle to choose correctly. Log every tool call with its arguments and result for debugging and auditing. Never expose a tool that can do something you would not want an automated process to do.

Failure modes

The common failures are infinite loops where the model keeps calling tools without converging, wrong tool selection, malformed arguments, and hallucinated tool names. Cap iterations at a reasonable number, such as ten, and force a final answer when the cap is reached. Validate arguments against the schema before execution. If the model calls a nonexistent tool, return an error message and let it retry. And watch for the model ignoring a tool result and re-asking the same question; a clear result format and a system prompt that instructs it to use results help.

When not to use agents

Not every task needs an agent. If the steps are known in advance, write a pipeline; it is cheaper, faster, and more reliable. Agents earn their complexity when the path to the answer is not known ahead of time and requires the model to decide what to do next based on intermediate results. Start with a deterministic pipeline and introduce agent behavior only where it is genuinely needed.

Resources: OpenAI function calling and Anthropic tool use.