OpenAI SDK
You run the agent loop with the OpenAI SDK; one call turns the model's function calls into the outputs to send back, each one shrunk the moment the tool returns. Both the Responses API and Chat Completions.
Responses API#
Add the expand tool to your tools, and replace the code that runs function calls and builds function_call_output items with one call:
Chat Completions#
The same, with tool messages:
Your tools stay as they are: runTool(name, args) returns a tool's raw output, text or any JSON value. The arguments arrive parsed.
What the call does#
For every function call in the response, in order:
- Runs your tool and keeps the raw output on your side.
- Shrinks the output for the model: noise removed, the relevant parts kept within the tool's budget. Small results, and results where a cut would save too little (under 1,000 tokens or 30%), pass whole.
- Answers
expanditself, from the original output. It never reaches your tools. - Reports failures as
Error: …text: a tool that throws, or arguments that aren't valid JSON.
Pass request, the user's request for the turn: with the text the model wrote before its calls, it tells relevance what the agent is after.
Good to know#
- The Responses API's
expandtool is not strict, because itsquestionis optional. UseexpandToolResponsesthere, andexpandToolfor Chat Completions. - Several function calls in one response run in parallel.
- Some models accept function tools on Chat Completions only with reasoning off: if the API refuses your tools, set
reasoning_effortto"none"or use the Responses API.