arthrop0d

Brief articles on varied topics.

How LLM Tool Calls Execute

ai agentsartificial intelligenceapi design

An LLM tool call is a structured request from a language model asking an external program to perform an operation. The model proposes the operation and its arguments; an executor—your application or an agent framework—validates those arguments, runs the tool, and sends the result back in a later model turn.

This solves a basic limitation of language models: a model can generate text, but it cannot directly query your database, call your internal API, read the current weather, or change a record. Without tool calls, an application has to guess when the model wants an action, extract parameters from prose, validate them, execute something, and paste the output back. That approach is fragile. A sentence such as “look up order 4815” is less reliable to parse than a declared operation with a named order_id field.

The tool declaration

Before generating a response, the application sends the model a list of available tools. Each tool has a name, a description, and an input schema: a machine-readable description of valid arguments, commonly expressed as JSON Schema.

A conceptual declaration might say:

{
  "name": "get_order",
  "description": "Retrieve an order by its identifier",
  "input_schema": {
    "type": "object",
    "properties": {
      "order_id": { "type": "string" }
    },
    "required": ["order_id"],
    "additionalProperties": false
  }
}

The declaration is not an implementation and is not an authorization policy. It tells the model what operation exists and what shape of input is expected. The executor still needs to decide whether the current user may retrieve that order.

The model emits a request

Given a user request such as “What is the status of order 4815?”, the model may return a normal text response, or it may return a tool call. A tool call contains the selected tool name and arguments that should conform to the declared schema.

Conceptually, the important part looks like this:

{
  "tool_name": "get_order",
  "arguments": { "order_id": "4815" },
  "call_id": "call_7f2"
}

The call_id correlates the eventual result with this particular request. Real provider APIs wrap these fields in provider-specific message structures, and the exact names differ. The important boundary is consistent: the model is producing data that describes an action, not performing the action itself.

A model can still produce an invalid call. It may omit a required field, use the wrong type, invent a tool name, or put malformed JSON in the arguments. “Structured output” improves the odds; it does not make model output trusted input.

Validation and execution

The application receives the model response and passes the proposed call to an executor. The executor normally performs several checks in order:

  1. Confirm that the tool name is one the application exposed.
  2. Parse the arguments as data rather than treating them as executable code.
  3. Validate them against the input schema.
  4. Apply application-specific checks such as authentication, authorization, limits, and allowed resource ownership.
  5. Invoke the implementation with the validated values.
  6. Bound the work with timeouts, cancellation, and resource limits.

Schema validation answers questions such as “is order_id present and a string?” It does not answer “does this user own order 4815?” or “should this operation be allowed at this time?” Those are executor responsibilities.

For a read-only lookup, execution might call an internal service or database. For a write operation—sending an email, deleting a record, or charging a card—the executor should also apply the safeguards used by any other API endpoint. The model's request is an untrusted proposal, even when it appears to follow the schema.

Returning the result in the next turn

The executor does not usually replace the model's original answer with the tool output. Instead, it appends a new conversation message containing the tool result and sends the conversation to the model again. That second model turn can interpret the result, combine it with the user's request, and produce a human-facing answer or request another tool call.

A conceptual sequence is:

user:       What is the status of order 4815?
assistant:  tool call get_order({"order_id":"4815"})
tool:       {"status":"shipped","tracking":"ZX91"}
assistant:  Order 4815 has shipped. Tracking number: ZX91.

The result message must be associated with the correct call, usually through the call ID or an equivalent provider field. It should contain a serializable representation of the outcome—not a live database connection, an exception object, or an unbounded internal response.

The model sees the tool result as new context. It does not gain a permanent connection to the tool, and it does not automatically know whether the result is trustworthy beyond what the application tells it. Keep result payloads focused: include the fields needed for the next decision and redact secrets or unrelated customer data.

Failures are part of the protocol

Several different failures can occur, and they should not be collapsed into “the model failed.”

The executor should turn recoverable failures into an explicit tool-result message, such as an error category and a short explanation, so the model can ask for a missing value or tell the user that an operation could not be completed. Do not silently omit the result: the model may otherwise respond as if the action succeeded.

Retries need equally explicit rules. A model may retry after learning that an argument was invalid. The executor may retry a transient network timeout. Those are different retry loops and should have separate limits, logging, and policies. Automatic retries are safe only when the operation is idempotent—repeating it has the same effect as doing it once—or when the tool uses an idempotency key. Retrying a read is usually simpler than retrying “send this email” or “charge this card.”

In production logs, the tool name, call ID, validation outcome, authorization decision, duration, retry count, and final result category make this lifecycle visible. The error message that says “invalid tool arguments,” “tool call timed out,” or “maximum agent steps exceeded” is reporting one of these explicit boundaries—not a mysterious behavior inside the model.

← All articles