← All articles

AI & LLMs

How an LLM decides to call your code

2026-06-27 · 5 min read

A language model can write you a beautiful function to fetch the weather. What it can't do — on its own — is run it. So how do assistants end up checking your calendar, searching a codebase, or placing an order? They use tools. And the model's only job is to decide which one.

The model can't actually run anything

A language model produces one thing: text. A "tool" is just a function you describe to it — a name, a sentence about what it does, and the inputs it expects. The model can't execute it. What it can do is emit a structured request that says "call get_weather with city = Pune." Your application sees that request, runs the real function, and hands the answer back.

The loop

The tool-use loop You ask Model picks a tool Your code runs it Model reads result Answer to you …and the model can loop back to call another tool before answering
The model chooses the tool and the arguments. Your code does the actual work.

Step by step, for "what's the weather in Pune?":

  1. You send the question plus a list of the tools available.
  2. The model replies with either a normal answer or a tool call — structured data naming the function and its arguments.
  3. Your application runs the real function and gets "31°C, humid."
  4. You feed that result back into the model.
  5. The model turns it into a sentence — or decides it needs another tool and goes around again.
The gist

The model decides which tool and what arguments. Your code does the doing. The model never touches your systems directly — it only ever asks.

Why this matters

  • It turns a text predictor into something that can act — search, calculate, book, query a database.
  • You control the menu. The model can only reach the tools you expose — which is exactly how MCP servers package those tools up in a standard way.
  • Never trust the arguments blindly. The model can pick the wrong tool or bad inputs. Validate before you run anything that matters.

That's the whole trick behind "AI agents": a plain request-and-reply loop, where one side happens to be very good at choosing the next move.


LLMTool useFunction callingAgents
← Back to the blog