Skip to content
TypeSafe AITypeSafe console
IntroductionFunction calling

COOKBOOKS

Function calling

Put a typed decision in front of a tool call.

The pattern

A tool call has two decisions: which function to use, and which arguments are allowed. A typed result can keep those choices explicit while application code controls execution. Use a closed set of function names and validate each argument before dispatch.

This original example routes a support action. It demonstrates the composition and code layout of the public cookbook without repeating its trading example.

A narrow question

Keep the function choice separate from the argument extraction. A request for account access should not also decide whether an irreversible action is authorized.

from dataclasses import dataclass
from typing import Literal

@dataclass(frozen=True)
class SupportAction:
    function: Literal["open_ticket", "ask_for_details"]
    team: Literal["account", "billing", "product"]
    urgent: bool

# Original deterministic UI fixture, not an SDK response.
action = SupportAction(
    function="open_ticket",
    team="account",
    urgent=True,
)

Ready to copy

Dispatch in application code

The application owns the branch. The local fixture maps a typed result to a message, so the page can demonstrate the result without contacting a help desk or model.

def describe_next_step(action: SupportAction) -> str:
    if action.function == "ask_for_details":
        return "Ask the visitor for missing details."
    priority = "urgent" if action.urgent else "normal"
    return f"Prepare a {priority} ticket for {action.team} support."

print(describe_next_step(action))
# Prepare an urgent ticket for account support.

Ready to copy

Uncertain or incomplete results

Ask for clarification when required arguments are missing or confidence is below the threshold chosen for the workflow. Keep the original input available so a person can recover without re-entering it.

Run the local fixture

Open the homepage decision specimen to try account, billing and unknown requests. Its output is deterministic and stays in this browser.

Open the local decision specimen
Was this page helpful?Choose a local response. Nothing is sent.
Type a page name. Results stay inside this reference.
Enlarged captured poster of the public TypeSafe Smart Home Demo
Still image from the public Loom embed. Open the poster link on the page to watch the original video.