Tool Use Pattern (Function Calling)
The agent asks for a tool; YOUR code actually runs it
Picture a chef shouting orders to a kitchen: "Two coffees, table four!" The chef doesn't make the coffee — the barista does, then hands it back. Tool Use (also called Function Calling ) works the same way: the LLM doesn't run code or browse the web itself. It outputs a structured request — a tool's name and arguments — and your program executes it and feeds the result back.
Key points
- The LLM REQUESTS a tool; it does NOT run the tool itself.
- The request is structured: a tool name plus arguments (usually JSON).
- Your code runs the function and returns the result to the LLM.
- This is how agents touch the real world: search, math, databases, APIs.
The one-line definition
Tool Use (Function Calling) is a pattern where you describe available tools to the LLM, and instead of answering directly it returns a structured call (tool name + arguments). Your code executes that call and returns the result, which the LLM then uses to continue.
Note: LLM picks the tool and the arguments. Your code does the running.
Who does what (the most important picture)
┌─────────────────┐ ┌──────────────────┐ │ LLM │ │ YOUR CODE │ │ (the brain) │ │ (the runtime) │ └────────┬────────┘ └────────┬─────────┘ │ │ │ 1. "call get_weather(city='Mumbai')" │ │ ───────────────────────────────────────►│ │ (just a REQUEST, not execution) │ │ │ 2. actually │ │ runs the │ │ function │ 3. result: { "tempC": 31 } │ │ ◄───────────────────────────────────────│ │ │ ▼ │
- LLM writes the final │
answer using the result │
The full Tool Use round-trip
You give the LLM a goal + a list of tool descriptions │ ▼ ┌────────────────────────────────────────────────────┐ │ LLM decides: "I need the weather tool." │ │ Returns: { tool: "get_weather", │ │ args: { "city": "Mumbai" } } │ └───────────────────────┬────────────────────────────┘ │ your code parses + executes ▼ result = get_weather(city="Mumbai") → 31°C │ hand result back to LLM ▼ ┌────────────────────────────────────────────────────┐ │ LLM: "It's 31°C in Mumbai right now." ✅ │ └────────────────────────────────────────────────────┘
The 4 pieces of Tool Use
- Tool schema — A description of each tool: its name, what it does, and its argument shape. Example: get_weather(city: string) — returns current temperature.
- The call request — What the LLM returns instead of an answer: a tool name plus arguments. Example: { "tool": "get_weather", "args": { "city": "Mumbai" } }
- The executor — YOUR code that parses the request, runs the real function, and captures the result. Example: result = TOOLS[name](**args)
- Result feedback — The function's result is returned to the LLM so it can finish the answer. Example: Append { "tool_result": 31 } to the conversation.
A tiny code example (read it like English)
You hand the LLM a list of tool schemas. If it returns a tool call, you look the function up in your own registry, run it, and send the result back. The LLM never executes anything itself.
def get_weather(city): # a REAL function you own
return {"tempC": 31}
TOOLS = {"get_weather": get_weather} # your tool registry
reply = llm(goal, tools=TOOL_SCHEMAS) # LLM may ask for a tool
if reply.tool_call: # it wants a tool
name = reply.tool_call.name # e.g. 'get_weather'
args = reply.tool_call.args # e.g. {'city': 'Mumbai'}
result = TOOLS[name](**args) # YOUR CODE runs it
final = llm(goal, tool_result=result) # LLM uses the result
print(final.answer)
When should you give the agent tools?
| Scenario | Recommendation | Why |
|---|---|---|
| The model needs live or private data (DB, API, search) | ✅ Use Tool Use | Tools fetch real data the model cannot know on its own. |
| The task needs exact computation (math, code, dates) | ✅ Use Tool Use | A calculator/code tool is precise; the LLM alone is shaky at arithmetic. |
| Pure language tasks (rewrite, summarise, translate) | ❌ No tools | The model can do these directly; tools add complexity. |
| You can't validate the tool's inputs/outputs | ⚠️ Be careful | Unvalidated tool calls (especially write actions) are risky. |
Tool Use mistakes (intermediate)
| Mistake | Consequence | Fix |
|---|---|---|
| Thinking the LLM runs the tool itself. | You never write the executor, so nothing actually happens. | Remember: the LLM only REQUESTS; your code parses and runs the function. |
| Trusting tool arguments blindly. | Bad/malicious args reach your DB or shell — a real security hole. | Validate and sanitise every argument before executing the tool. |
| Vague tool descriptions. | The LLM calls the wrong tool or passes wrong arguments. | Write clear names, descriptions, and typed argument schemas for each tool. |
| Forgetting to return the result to the LLM. | The agent never sees the outcome and can't finish the answer. | Always feed the tool result back into the conversation before asking for the final reply. |
Remember these 3 lines
- Tool Use = LLM asks for a function call; your code runs it and returns the result.
- The LLM never executes anything — it only emits a name + arguments.
- Validate every argument; clear tool schemas make the LLM pick correctly.
Key takeaways
- Tool Use (Function Calling) has the LLM emit a structured call (tool name + arguments) instead of an answer.
- Your application code parses and executes the function — the LLM never runs the tool itself.
- The tool's result is returned to the LLM so it can produce the final answer.
- Write clear tool schemas and always validate arguments before executing, especially for write actions.
Frequently Asked Questions
What is Tool Use Pattern?
Picture a chef shouting orders to a kitchen: "Two coffees, table four!" The chef doesn't make the coffee — the barista does, then hands it back. Tool Use (also called Function Calling ) works the same way: the LLM doesn't run code or browse the web itself.
How does Tool Use Pattern work?
Tool Use (Function Calling) is a pattern where you describe available tools to the LLM, and instead of answering directly it returns a structured call (tool name + arguments). Your code executes that call and returns the result, which the LLM then uses to continue.
What are the key takeaways about Tool Use Pattern?
Tool Use (Function Calling) has the LLM emit a structured call (tool name + arguments) instead of an answer. Your application code parses and executes the function — the LLM never runs the tool itself. The tool's result is returned to the LLM so it can produce the final answer. Write clear tool schemas and always validate arguments before executing, especially for write actions.
Related topics
Practice this on DevInterviewMaster
Read the full Tool Use Pattern (Function Calling) breakdown with interactive demos, quizzes, and Hinglish notes.
800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.