Tool use — study guide

The concept's fragments, read in order.

For your capstone Take your own app to production

The assistant that triages your app's alerts is useful only once it can look at the app, and tools are how it looks. Each lookup is a request your ops program checks and runs with narrowly scoped credentials of its own, so the limit that counts is the one your systems enforce, not one written in a prompt.

Its lookup tools, query_logs, get_metric and list_recent_deploys, are all read-only. A query_logs call for the last 15 minutes hits a log service answering 503, so your program sends back log service unavailable (503), retry later as an error result and keeps the stack trace in its own logs. There is no restart_service tool, and no run_shell.

The model asks, your program acts

A language model takes text in and produces text out. On its own it cannot check today's forecast, read a file, look up a record or send a message; everything it says comes from what it learned in training and what is in its input right now. Tool use is how it reaches past that. You describe some functions to the model, and instead of answering in prose it can answer with a request to call one of them.

The precise version matters, because the loose version gets it wrong. The model does not run anything. It emits a structured request, a tool name and a set of arguments, as data. Your program reads that request, decides whether to honor it, runs the function itself, and sends the result back to the model as more input, like asking for a book from a library's closed stacks: you write down exactly which item you want, a librarian decides whether to fetch it and does the fetching, and you read what comes back across the counter. The model then writes its answer with the result in front of it. Some providers also offer built-in tools that run on their own servers; the shape is the same, only who executes the call moves.

That division of labor is the whole concept. The model contributes a judgment about which call would help and with what arguments. Your program contributes everything that touches the world, and with it every check. The rest of this concept is the mechanics of that split: how a tool is described, what one round trip looks like, how to check a request before running it, how to report failures, how to treat what comes back, and why tools that change things deserve more care than tools that only look.

Describing a tool to a model

one tool definition, read by both sides tool definition name get_forecast description Forecast for one city, next few days. Use for upcoming weather, not past. parameter schema (JSON Schema) city: string, required model reads all three to pick a tool, fill it in whole definition sent as input your program checks the arguments validates each request the description is prompt text: it decides when the tool gets picked
A tool definition has three parts, a name, a description and a parameter schema; the whole definition is sent to the model as input, so the description decides when the tool gets picked, and your program checks each request's arguments against the schema.

A model can only request tools it has been told about, and what it is told is a tool definition with three parts. A name, such as get_forecast. A plain-language description of what the tool does and when to use it: returns the forecast for one city over the next few days; use it for upcoming weather, not past weather. And a parameter schema, usually written in JSON Schema, saying which arguments the tool takes, what type each one is and which are required, here a single required string called city. It is the same kind of schema a program uses to validate any incoming document, and it does two jobs: it tells the model what shape its request must have, and it tells your program what to check when the request arrives.

The definitions travel with the request, as part of the model's input, alongside the instructions and the conversation. They are read the way the rest of the prompt is read, and they generally count toward the input size that providers limit and bill, though the exact accounting varies. That makes the description prompt text, not documentation for a human. The model decides whether to call a tool, and which one, by comparing what it has been asked with the names and descriptions it was given, like a newcomer choosing a drawer in an unfamiliar workshop by its label alone: a clear label gets the right drawer opened, and a vague one gets the wrong one. Two tools with overlapping descriptions get confused with each other; a description that never says when to use its tool invites the model to miss it or reach for it at the wrong time. Descriptions on the parameters do the same job for arguments: say that a date is written YYYY-MM-DD, and you are more likely to get one in that format.

Interfaces also let your program decide how much choice the model has on a given request. Typical settings let the model decide whether to call a tool at all, require it to call some tool, force one particular tool, or switch tools off. The exact switches differ between providers, but the idea is common to them: offering a tool is an option for the model, not an obligation, unless you make it one.

One round trip

one round trip: who sends what, and who runs it model your program tool 1. conversation + tool definitions 2. request: get_forecast, city Lisbon, id call_1 checks the request 3. runs the call 4. output 5. conversation, call and result, id call_1 6. answer, or another request the model only asks; your program runs the call (for built-in tools, the provider does)
One tool-use round trip as a sequence between the model, your program and the tool: the model only sends a request naming the tool, its arguments and a call id; your program checks it, runs the tool and sends the result back under the same id; the model never runs the call.

A round trip starts with your program sending the model the conversation so far plus the tool definitions. The model replies, and instead of a final answer the reply contains a tool-call request: the name of the tool, the arguments as a small JSON object, and an id that names this particular call. Asked whether it will rain in Lisbon tomorrow, the model might reply with a request for get_forecast with city set to Lisbon and the id call_1. Nothing has happened yet. The reply is a piece of data sitting in your program.

Your program takes it from there. It checks the request, then runs the real function, which might query a database, call an HTTP API or read a file, and collects the output. Then it sends the model a new request: the conversation, plus the model's own tool-call message, plus a tool-result message carrying the output and the same id, call_1. The id is how the model knows which request this result answers. The model reads the result and replies again, this time with an answer in prose or with another request.

One reply can carry several requests at once, each with its own id, when the calls do not depend on each other, such as forecasts for two cities. Your program decides whether to run them one after another or at the same time, and it sends back one result per call, matched to its request by id rather than by position.

Follow the arrows and one fact stands out: every step that touches the outside world starts in your program. The model proposes; your program executes. If the model reads a result and asks for another call, that is the start of a loop, and running the loop well is a topic of its own built on this one exchange.

Check the request before you run it

three checks stand between a request and a run tool-call request from the model 1. a tool you offered, allowed in this context? yes 2. arguments parse and match the schema? yes 3. values make sense for this user, right now? yes run the tool error result sent back to the model nothing runs no no no only a call that passes all three is executed
A requested tool call passes three checks in order: a tool you offered and allowed here, arguments that match the schema, and values that make sense for this user now; only a call that passes all three runs, and a failure at any check becomes an error result sent back to the model.

A tool-call request is model output, which makes it structured output with everything that implies: it usually parses and usually matches the schema, and usually is not always. A model can name a tool that does not exist, leave out a required argument, or put a word where a number belongs. Some interfaces offer a strict mode that constrains the arguments to the schema as they are generated, which removes much of that; how complete the guarantee is depends on the provider, the model and which schema features are supported.

Even a perfectly shaped request can be wrong. A schema can say that invoice_id is a string; it cannot say that this invoice belongs to the person asking. So the check before running a call has three layers. First, the tool must be one you offered and one that is allowed in this context, matched against an explicit list of names, not looked up by whatever name arrived. Second, the arguments must parse and validate against your own schema: types, required fields, allowed values, ranges. Third, the values must make sense here and now: the record belongs to this user, the amount is within a limit, the path stays inside the directory the tool is meant to touch.

Only a call that passes all three runs. A call that fails any of them does not crash the program and does not half-run; it becomes an error result sent back to the model. The model chose the arguments, but it does not get to decide whether they are acceptable. That decision belongs to code, which does not change its mind when the input is persuasive.

Failures go back as results

Plenty can go wrong between a request and its result. The arguments fail validation, the tool name is unknown, the HTTP API behind the tool answers 404 or 503, or the call simply times out. Whatever happened, the model is waiting for a result tied to its call id, and the most useful thing your program can send is an honest one: an error, delivered as the tool result, and marked as an error where the interface has a way to say so.

A good error result is short and specific about what failed and what would fix it: date must be YYYY-MM-DD, got: next Friday, or no invoice A-1042 on this account. The model reads it like any other input and can try a corrected request or tell the person what went wrong, like a form handed back with the faulty box marked, so it can be corrected and resubmitted instead of disappearing without a word. A bare error gives it nothing to work with. An empty result is worse, because it looks like a call that succeeded and found nothing.

The status code is worth passing on in words. Most 4xx codes say the request was wrong, so a corrected request may succeed. The exceptions matter: 429 means slow down and retry later, and 401 or 403 means the call is not permitted, which no rewording fixes. A 5xx or a timeout says the service failed; different arguments will not help, though a later retry might. Two things stay out of the result. Stack traces and internal details belong in your logs, not in the model's input, where they take up space and may get repeated to a user. And secrets never go in: a connection string or token inside an error message has just become part of the conversation.

The result is just more input

Once your program sends a result back, it is text in the model's input, read the same way as the instructions and the conversation. Nothing marks it as established fact. The model can misread a field, mix up two records or restate a number wrongly, so a result shaped for easy reading earns its keep: consistent field names, units stated, and only the fields that bear on the question.

Size matters too. A result stays in the model's input for the rest of the exchange, and a raw dump of every field an API returned crowds out the parts that matter and costs more to process. Guidance on writing tools for models recommends returning small, relevant results, using readable names rather than opaque ids unless a later call will need the id, and paging or truncating large results while saying plainly that something was cut and how to get the rest.

A result is also untrusted input. It came from a web page, a file, a database row or another service, and any of those can contain text phrased as an instruction, which a model may follow because instructions and data reach it through the same channel. How that goes wrong is a subject of its own; the rule to carry from here is that what a tool returns gets no more authority than what a stranger typed.

Looking versus changing

looking and changing take different roads validated tool call read-only tool fetch, search, read changes something send, write, delete, pay looks acts runs straight away safe to repeat run a person confirms consequential actions first, then it runs once hold result to model a repeated read changes nothing; a repeated change happens twice
A validated tool call takes one of two paths: a read-only tool runs straight away and is safe to repeat, while a tool that changes something, when the action is consequential, waits for a person to confirm that exact action and then runs once; either way the outcome returns to the model as a result.

Tools come in two kinds that deserve different treatment. Read-only tools look: fetch a forecast, search some documents, read a record. Calling one twice costs a little time and changes nothing, so a model can be allowed to call them freely, and a failed call can be retried. Tools with side effects act: send a message, write a file, update a record, start a payment. Asking again does not undo them, and calling one twice may mean doing the thing twice, like the difference between checking a bank balance, which you can do ten times without consequence, and sending a transfer, which happens again every time you press the button.

Tools that act call for three habits. Make them narrow: a reply_to_ticket that takes a ticket id and a message, rather than a general send_email that can write to anyone, and a specific operation rather than a general run_shell or run_sql, because an open-ended tool hands the model every action that tool can reach. Put a confirmation step in front of the consequential ones: your program shows a person exactly what is about to happen and runs it only after they approve that action. And be deliberate about repeats: when a call times out and you cannot tell whether it went through, a blind retry may do it a second time, so send an idempotency key the receiving system can recognize, or check whether the action happened, before retrying.

Underneath those habits, the limit that counts is the one enforced by the systems being touched, not the one written in a prompt. OWASP's guidance for applications built on language models names the failure excessive agency, meaning too many tools, too much permission or too much autonomy, and recommends offering only the tools a job needs, giving each the minimum permissions, acting with the requesting user's own permissions, requiring a person's approval for high-impact actions, and enforcing authorization in the downstream system rather than leaving the model to decide. An agent working on your infrastructure is held to the same rule through narrowly scoped credentials of its own.

Where a model touches the world

Without tools, a model can only produce text about the world. With them, it can read the current state of something and, if you let it, change it. That is the step from a model that answers questions to a model that is one part of a working system, and every project that wires a model to real things takes it: a pipeline whose steps fetch, transform and store; a robot whose planner asks for a sensor reading before it decides; an app whose assistant looks up an account before it replies.

In each of them the shape is the same round trip. The model proposes a call, your program checks it and runs it, and the result comes back as input. So the program around the model is where the safety lives. It decides which tools exist, validates every request, reports failures honestly and holds the permissions. A wrong request from a model is ordinary and expected; a wrong request that your program runs without checking is a bug in your program.

Get the single round trip right and everything larger stands on it. A model that reads a result and asks for another call is inside an agent loop, which your program runs, and every check that protects one call protects each turn of that loop.

Next on your path to Take your own app to production: