function calling

The LLM API feature that allows models to emit structured JSON tool calls instead of free text, enabling reliable tool invocation in agent systems.

Syntax

ai-agents
// Model outputs:
{ tool_call: { name: "fn_name", arguments: { ... } } }

Example

ai-agents
// Handling function calls:
const response = await llm.create({ tools, messages });

if (response.stop_reason === "tool_use") {
  const toolUse = response.content.find(b => b.type === "tool_use");
  
  // Execute the requested tool:
  const result = await tools[toolUse.name](toolUse.input);
  
  // Feed result back to model:
  messages.push({ role: "user", content: [{ type: "tool_result", tool_use_id: toolUse.id, content: JSON.stringify(result) }] });
  // Continue loop...
}