Agentic workflows enable the model to make multiple sequential tool calls for complex multi-step reasoning. The model autonomously decides which tools to use, chains them together, and iterates until it has sufficient information to answer the question.
Because every iteration depends on the model emitting valid JSON in tool_calls[i].function.arguments, a single malformed argument can break the entire chain. Add post_processing_steps: [{"type": "json-repair"}] to your request to automatically repair common JSON errors before they reach your loop. See Post-processing.
Enable the model to make multiple sequential tool calls:
import requestsheaders = { "authorization": "<YOUR_API_KEY>"}conversation_history = [ {"role": "user", "content": "Where does Sarah work and what city is that in?"}]max_iterations = 10iteration = 0while iteration < max_iterations: iteration += 1 response = chat(conversation_history, model) choice = response["choices"][0] if choice.get("message", {}).get("tool_calls"): # Execute tools and add results to history handle_tool_calls(choice["message"]["tool_calls"], conversation_history) # Continue loop - model can make more tool calls continue else: # Model has final answer print(choice["message"]["content"]) break
You: Where does Andrew live?🔧 Calling: search_employees(first_name="Andrew")✅ Result: Found 1 result🔧 Calling: search_tavily(query="33186 zip code location")✅ Result: The zip code 33186 is located in Miami, Florida...Assistant: Andrew lives in Miami, Florida (zip code 33186).
An array of completion choices. Typically contains one choice.
choices[i].message
object
The message object containing the model’s response.
choices[i].message.role
string
The role of the message, typically "assistant".
choices[i].message.content
string
The text content of the model’s response. May be null when calling tools.
choices[i].finish_reason
string
The reason the model stopped generating. Common values: "stop", "length", "tool_calls" (OpenAI models), "tool_use", "end_turn" (Claude models). LLM Gateway passes the provider-native value through; client code should accept either family.
choices[i].message.tool_calls
array
Present on assistant message choices when the model calls a tool. Each tool call appears in its own choice with the calling content nested under message.
request
object
Echo of the request parameters (excluding messages).