Agents, Tool Use & MCP·Question 4 of 90BasicAI-75

How does function/tool calling actually work mechanically, end to end?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

You send the model tool definitions alongside the conversation; the model, instead of (or before) replying with prose, emits a structured message meaning "call tool X with arguments Y"; your code executes it and sends back a result message; the model continues with that observation.

Full worked answer with diagram

You send the model tool definitions alongside the conversation; the model, instead of (or before) replying with prose, emits a structured message meaning "call tool X with arguments Y"; your code executes it and sends back a result message; the model continues with that observation.

Concretely:

  1. Definitions: each tool is a name, a natural-language description, and a JSON Schema for parameters. These are serialized into the model's context (they consume input tokens - 20 verbose tools can easily be thousands of tokens per call).
{
  "name": "get_weather",
  "description": "Get current weather for a city. Use for weather questions only.",
  "input_schema": {
    "type": "object",
    "properties": {"city": {"type": "string"}},
    "required": ["city"]
  }
}
  1. The "call": the model outputs a tool-use block - tool name, JSON arguments, and a unique call ID. Nothing executes; this is just structured text the model was post-trained (SFT + RL on tool-use trajectories) to produce. Providers can additionally constrain decoding so arguments are guaranteed schema-valid (e.g., OpenAI structured outputs with strict: true).

  2. Execution: your runtime parses the block, validates arguments (never trust them blindly - models fabricate paths and enum values), runs the actual function, and captures output or error.

  3. Result return: you append a tool-result message referencing the call ID, then call the model again. With parallel tool calls, one assistant turn can contain several tool-use blocks; you return one result per ID.

  4. Termination: eventually the model replies with plain text and no tool calls.

Common misconception to preempt: there's no hidden channel - the whole mechanism is messages in, structured messages out, and your code is the only thing with side effects.

Worth sketching. The call ID threading through both directions is the detail that proves you have implemented this rather than read about it.

sequenceDiagram
    participant R as Your runtime
    participant M as Model
    participant T as Tool
    R->>M: messages + tool schemas
    M->>R: tool_use block with id, name, args
    R->>R: validate args, check permission
    R->>T: execute the real function
    T->>R: output or error
    R->>M: tool_result carrying the same id
    M->>R: plain text, no tool calls

What a good answer covers

  • You send the model tool definitions alongside the conversation; the model, instead of (or before)…
  • Definitions: each tool is a name, a natural-language description, and a JSON Schema for parameters
  • The "call": the model outputs a tool-use block - tool name, JSON arguments, and a unique call ID
  • Execution: your runtime parses the block, validates arguments (never trust them blindly - models…
  • Result return: you append a tool-result message referencing the call ID, then call the model again

Key terms: Definitions · The "call" · Execution · Result return · Termination

Interviewer follow-ups to expect

  1. What happens if you return tool results in a different order than the calls?

  2. Why do providers use constrained decoding for arguments but not for choosing which tool to call?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep