toolboxTool Calling

Function calling and tool integration with the Responses API

The Responses API supports comprehensive tool calling capabilities, allowing models to call functions, execute tools in parallel, and handle complex multi-step workflows.

Basic Tool Definition

Define tools using the OpenAI function calling format:

const weatherTool = {
  type: 'function' as const,
  name: 'get_weather',
  description: 'Get the current weather in a location',
  strict: null,
  parameters: {
    type: 'object',
    properties: {
      location: {
        type: 'string',
        description: 'The city and state, e.g. San Francisco, CA',
      },
      unit: {
        type: 'string',
        enum: ['celsius', 'fahrenheit'],
      },
    },
    required: ['location'],
  },
};

const response = await fetch('https://llm.onerouter.pro/v1/responses', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer <<API_KEY>>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'o4-mini',
    input: [
      {
        type: 'message',
        role: 'user',
        content: [
          {
            type: 'input_text',
            text: 'What is the weather in San Francisco?',
          },
        ],
      },
    ],
    tools: [weatherTool],
    tool_choice: 'auto',
    max_output_tokens: 9000,
  }),
});

const result = await response.json();
console.log(result);

Tool Choice Options

Control when and how tools are called:

Tool Choice
Description

auto

Model decides whether to call tools

none

Model will not call any tools

{type: 'function', name: 'tool_name'}

Force specific tool call

Force Specific Tool

Disable Tool Calling

Multiple Tools

Define multiple tools for complex workflows:

Parallel Tool Calls

The API supports parallel execution of multiple tools:

Tool Call Response

When tools are called, the response includes function call information:

Tool Responses in Conversation

Include tool responses in follow-up requests:

The `id` field is required for `function_call_output` objects when including tool responses in conversation history.

Streaming Tool Calls

Monitor tool calls in real-time with streaming:

Tool Validation

Ensure tool calls have proper structure:

Required fields:

  • type: Always "function_call"

  • id: Unique identifier for the function call object

  • name: Function name matching tool definition

  • arguments: Valid JSON string with function parameters

  • call_id: Unique identifier for the call

Best Practices

  1. Clear descriptions: Provide detailed function descriptions and parameter explanations

  2. Proper schemas: Use valid JSON Schema for parameters

  3. Error handling: Handle cases where tools might not be called

  4. Parallel execution: Design tools to work independently when possible

  5. Conversation flow: Include tool responses in follow-up requests for context

Next Steps

  • Explore Reasoning with tools

lightbulbReasoningchevron-right
  • Review Basic Usage fundamentals

playBasic Usagechevron-right

Last updated