Function Calling

Structured JSON in, typed function call out.

The model emits a structured call — a named function with typed, schema-validated arguments — that the harness executes, feeding the result back into the model's context; the model decides which function to call, when, and with what arguments.

In practiceA calendar assistant uses function calling to invoke a `create_event(title, start, end, attendees)` function with validated arguments, rather than generating a free-text API call the harness must parse.

Level
04 · Production
Subdomain
Tool Integration
Also known as
Tool Calling, Structured Tool Use, Function Invocation
Trade-off
Structured control is gained at the expense of schema definition and integration effort.
Native in
OpenAI Agents SDK, LangGraph, Microsoft Agent Framework, Google ADK, AutoGen / AG2, CrewAI
The mechanism

How it works.

The model never executes anything. It writes a request in a fixed format, and the harness decides whether and how to carry it out.

1 · Before the run

  1. Describe each functionA name, a purpose and a schema for the arguments
  2. Send the descriptionsThey travel with every model call

2 · During the run

  1. The model emits a callA function name and arguments as JSON
  2. The harness validatesThe arguments are checked against the schema
  3. The harness executesThe real function runs outside the model
  4. The result goes backAs a new message, and the model continues
Schema
The description of the arguments: names, types and required fields. It is the contract both sides are held to.
Tool call
What the model outputs when it wants a function to run: a name and arguments, no prose.
Tool result
What the harness sends back after running the function. An error is a result too.
Walk it through

A real run, step by step.

Task“Book a review with Ana tomorrow at 10 for an hour.” The assistant has one function, create_event:
Illustrative run.
StepWhoWhat happens
1ModelCalls create_event with start "tomorrow 10am"
2HarnessRejects the call: start is not a date-time. The error goes back as the result
3ModelCalls again with start 2026-05-13T10:00 and end 11:00
4HarnessThe arguments are valid. The function runs and returns the event ID
5ModelAnswers the user: the review is booked for 13 May, 10:00 to 11:00
What this shows: the schema caught a value the calendar would have rejected or misread, and the model corrected itself because the error came back as a result. Nothing ran until the harness had checked the arguments. One action cost three model calls.
In code

The pattern in code.

Plain Pythonpython
TOOLS = [{
    "name": "create_event",
    "description": "Create a calendar event.",
    "parameters": {
        "type": "object",
        "properties": {
            "title": {"type": "string"},
            "start": {"type": "string", "format": "date-time"},
            "end": {"type": "string", "format": "date-time"},
        },
        "required": ["title", "start", "end"],
    },
}]

def run(messages):
    while True:
        reply = model(messages, tools=TOOLS)
        if not reply.tool_calls:
            return reply.text  # no call: this is the final answer
        for call in reply.tool_calls:
            try:
                # the harness checks, not the model
                args = validate(call.arguments, TOOLS)
                result = FUNCTIONS[call.name](**args)
            except ValidationError as error:
                # an error goes back as a result
                result = f"Invalid arguments: {error}"
            messages += [call, tool_result(call.id, result)]
Fit

Use it, or not.

When to reach for it

  • External actions or data sources must be integrated in a controlled manner.
  • Arguments need strict validation.
  • Tool usage should be observable and bounded.

When it backfires

  • The task can be solved without external actions.
  • The tool schema is unstable.
  • Free text interaction is more appropriate.
Pitfalls

Two ways this pattern will hurt you.

Schema drift breaks the contract silently

A tool's argument shape changes; the model keeps emitting the old shape; calls succeed parsing but execute with stale parameters.

Countermeasure

Validate every argument payload against the current schema at the harness, before dispatch. Reject and surface the mismatch — never coerce.

Arguments that pass the schema but are fabricated

The model invents a plausible id, path, or amount that matches the type and validates cleanly — then the call executes against a record that never existed or the wrong one.

Countermeasure

Validate arguments against real referents (existence and ownership checks), not just types; have the tool reject unknown ids rather than acting on a well-typed guess.

Threat exposure

What this pattern exposes

Adopting this pattern opens these attack surfaces. Each links to its entry in the threat model.

Full threat model →
Framework support

Where Function Calling is native.

OpenAI Agents SDKnative tool callingNative
LangGraphbind_toolsNative
Microsoft Agent Frameworkfunction toolsNative
Google ADKfunction declarationsNative
AutoGen / AG2tool-calling agentsNative
CrewAItool decoratorsNative

Search

Search patterns, frameworks, and pages.