How to Implement Tool Use in LLMs Step by Step
Tool use lets an LLM call functions like a search or a database lookup instead of guessing. Here is how to implement it, from the first tool to safe write actions.
On this page
Short answer
To implement tool use in an LLM, define each tool with a clear name, description and argument schema, wire a loop where your code runs the tool and returns the result to the model, and add validation, a call cap and logging. Start with one read-only tool, test it with real questions, and only then add write actions behind an approval step.
What is tool use in an LLM?
Tool use in an LLM is the ability of a language model to call a defined function, such as a search, a database lookup or a calculator, instead of answering only from the text it was trained on. The model decides which tool fits the question, sends structured arguments to it, reads the result back and then writes its answer using that result.
The key idea is that the model does not run the tool itself. Your application code runs the tool and hands the result back to the model. That separation is what makes tool use safe to build: the model can only call the tools you defined, with the arguments you allow, and your code decides what actually happens.
A support agent that checks an order status, a sales assistant that looks up a price, and a data helper that runs a query all use the same pattern. The difference from plain chat is that the answer is grounded in live data, so it is current and specific rather than a general guess.
For the mechanics of how the model picks a tool and how the call-and-response loop works, how tool use in LLMs works covers the loop step by step, while this article covers building it into a product.
Which tools should the model be allowed to call?
The safest rule is to start with read-only tools and add write actions only when a human or a check approves them. A model that can read an order record is useful on day one; a model that can refund an order needs guardrails, limits and logging before it goes live. The table lists the tool types most teams start with.
| Tool type | What it does | Risk level |
|---|---|---|
| Search | Finds documents or records by keyword | Low, read-only |
| Database lookup | Returns one record by ID | Low, read-only |
| Calculator | Computes totals, dates or rates | Low, deterministic |
| Calendar check | Reads availability or deadlines | Low, read-only |
| Create a draft | Prepares a message or ticket | Medium, needs review |
| Update a record | Changes a status or field | High, needs approval |
| Send a payment | Moves money or places an order | High, needs approval and limits |
Each tool should do one job and do it well. A single tool that searches, filters, sorts and exports is hard for the model to call correctly, because the arguments get tangled. Split it into a search tool and a separate export tool, and the model will pick the right one far more often.
Keep the total tool count small. Three to six tools cover most products. With twenty tools the model spends its effort choosing between them, and similar tools get confused with each other. If two tools overlap, merge them or make their names and descriptions clearly different.
How to write a tool definition the model can use
A tool definition has a name, a plain-language description and a schema that lists each argument, its type and whether it is required. The description is the most important part, because it is what the model reads when deciding whether to call the tool. Write it the way you would explain the tool to a new colleague.
Argument names should say what the value is, not how it is stored. Use order_id and customer_email, not ord_pk or cust_eml. A date range needs a start date and an end date, not one string the model formats by guessing.
Mark optional arguments as optional and give sensible defaults where they exist. A search tool with an optional limit argument that defaults to 10 results is easier to call than one that forces the model to choose a number every time.
Write the description to say when the tool should be used and when it should not. A line such as "Use this to look up a single order by its order number. Do not use it to list all orders" prevents the two most common mistakes: calling the wrong tool and calling a tool with the wrong kind of input.
Test each definition with five questions a user might ask and check which tool the model picks. If it picks the wrong one twice, the description is the problem. Rewrite it until the picks are right, and keep those questions as a regression set.
How to implement tool use in LLMs, step by step
Implementation is a sequence of small, testable steps: the model integration first, the guardrails second, and write actions last.
- Pick one read-only tool. Choose the single lookup your users ask for most, such as order status. Build only that tool first, so you learn the loop before adding complexity.
- Write the definition and the function. Define the name, description and schema, then write the function that runs it. It should validate its arguments and return a short, structured result, not raw data.
- Wire the loop. Send the question and the tool definitions to the model, run the tool it calls, send the result back, and let it answer. Log every call so you can debug what happened.
- Handle failures. Decide what happens when a tool errors, returns nothing or returns too much. Return a short error to the model and let it explain the problem, rather than crashing or inventing an answer.
- Add validation and limits. Check argument types and ranges in your code, cap result sizes, and limit tool calls per turn to three to five. A model that can loop without a cap runs up costs and time.
- Test, then add write actions. Run your test questions, fix the definitions that misfire, and only then add a write tool behind an approval step. Ship the read-only version first and measure it.
The cost side matters too: each tool call adds tokens and time. LLM tool use cost explains what drives the bill and how to keep it down.
Example (hypothetical): a support agent checks an order
Example (hypothetical): a support team handles 400 tickets a day, and 240 of them need an order lookup before the agent can answer. Without tool use, an agent opens the order system and spends 5 minutes per lookup. That is 240 lookups times 5 minutes, which is 1,200 minutes, or 20 hours of lookup time a day across the team.
With tool use, the model calls the order lookup tool, reads the status and delivery date, and drafts the reply. The agent spends about 30 seconds checking the draft and 1 minute on the reply itself, which is 1.5 minutes per ticket. For 240 tickets that is 360 minutes, or 6 hours a day.
The saving is 20 hours minus 6 hours, which is 14 hours a day of agent time freed for the tickets that actually need a person. The tool itself is simple: one lookup by order number, returning status, items and delivery date. The team ships that one tool first and adds a second, a refund draft tool, only after a month of clean logs.
The example also shows why logging matters. If the model calls the lookup with the wrong order number, the log shows the bad argument and the team fixes the description. Without logs, the same mistake repeats and nobody knows why the answers are wrong.
How do you handle failures, retries and bad output?
Tools fail, and the design should expect that. A database can be slow, an API can return an error, and a model can call a tool with a missing argument. Each case needs a planned response, not a crash.
When a tool errors, return a short error message to the model in the same format as a normal result, and let the model tell the user what went wrong and what to try next. Do not let the model guess the missing data: a message such as "the order system is unavailable, try again in a few minutes" is honest and useful, while an invented order status is not.
Retries should be limited and deliberate. Retry a failed call once, and only if the failure looks temporary, such as a timeout. Retrying a call that failed because the arguments were wrong will fail the same way, and each retry costs time and tokens. Log the retry so you can see which tools fail most often.
Bad output is usually a sign that the tool returned too much. If a lookup returns fifty fields and the model needs three, trim the result in your code before sending it back. Smaller, cleaner results mean fewer mistakes and lower token costs. If the model still misreads the result, rewrite the tool description to say exactly what the result contains.
For the security side, AI security covers how to keep tool access scoped so a compromised prompt cannot reach tools it should never call.
What are the common mistakes?
The most common tool-use mistakes are too many tools, vague descriptions and write access without approval.
- Too many tools. Twenty similar tools confuse the model. Start with three to six, each doing one clear job.
- Vague descriptions. A description that says "gets data" tells the model nothing. Say what data, from where, and when to use it.
- Write access without approval. A model that can change records or send money without a check will eventually do so wrongly. Gate write actions behind approval or review.
- No argument validation. Trusting the model's arguments without checking them lets bad input reach your systems. Validate types and ranges in your own code.
- No call cap. A model that can loop through tool calls without a limit can burn time and tokens. Cap calls per turn at three to five.
- Skipping the logs. Without a log of calls, arguments and results, you cannot debug a wrong answer. Log everything from day one.
- Returning raw data. Dumping a full database row into the model wastes tokens and invites mistakes. Return only the fields the answer needs.
If the team is building the model side from scratch rather than wiring tools into an existing product, LLM development services cover the model, the tools and the guardrails as one build.
Key takeaways
- Start with one read-only tool and prove the loop before adding more.
- The tool description is what the model reads, so write it precisely.
- Validate arguments and cap tool calls per turn in your own code.
- Return short, trimmed results to the model, not raw database rows.
- Gate every write action behind approval or human review.
- Log every call, argument and result so wrong answers can be debugged.
Frequently asked questions
What is tool use in an LLM?
Tool use lets a language model call a defined function, such as a search or a database lookup, and use the result in its answer. Your code runs the tool; the model only decides which tool to call and with which arguments.
How many tools should an LLM have?
Three to six tools cover most products. More tools confuse the model, because it spends its effort choosing between similar options. Merge overlapping tools and keep each one doing a single clear job.
How do you stop an LLM from calling the wrong tool?
Write precise descriptions that say when to use the tool and when not to, and test with real questions. If the model picks wrong, rewrite the description. Validate arguments in your code so a bad call cannot reach your systems.
Should an LLM be allowed to change records?
Only behind an approval step or a human review queue. Read-only tools can ship first; write actions need limits, logging and a way to undo mistakes. A model with unchecked write access will eventually act on a wrong input.
What happens when a tool call fails?
Return a short error message to the model in the normal result format and let it explain the problem to the user. Retry once if the failure looks temporary, such as a timeout. Never let the model invent the missing data.
How do you test tool use before launch?
Write five to ten real user questions per tool and check which tool the model picks and whether the answer is right. Keep those questions as a regression set, and log every call in production so failures can be traced.