v0.10.5

Tool Calls

Last updated


How to declare tools and return tool results across every provider.

Declaring tools


Tool definitions use the OpenAI shape, with Parameters as json.RawMessage so a JSON Schema goes in verbatim:
  1. tools := []llmrouter.Tool{{
  2. Type: "function",
  3. Function: llmrouter.ToolFunction{
  4. Name: "get_weather",
  5. Description: "Get the current weather for a city",
  6. Parameters: json.RawMessage(`{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}`),
  7. },
  8. }}

Returning results


Results come back as Role: "tool" plus ToolCallID, where ToolCallID is the ToolCall.ID from the assistant turn, passed back unchanged. Gemini has no call IDs of its own: the adapter sets ToolCall.ID to the function name and sends ToolCallID back as the functionResponse name. Gemini additionally attaches ToolCall.ThoughtSignature, which must be echoed back verbatim or the next turn is rejected.

x_search is a tool that runs on xAI's side. During one request, Grok can search X by keyword or meaning, look up users, and read whole threads, then answer with what it found. Your code never executes it and never sees a tool call for it.
ItemBehavior
Providersgrok (API key) and grok-oauth
ModelsEvery grok-* chat model; imagine image and video models are excluded (xai.SupportsXSearch)
When it is attachedOnly when the request already declares at least one function tool; a request without tools sends none
What you receiveThe final answer text and your own function calls; search steps arrive as custom_tool_call items, which both Send and SendStream drop
CostxAI bills each search separately from tokens, and one question can trigger many: a single test question made 3 to 27 searches depending on the model
Turning it offNo option yet; send the request without tools, or use a provider other than Grok

  1. agent, _ := router.New(router.Config{
  2. Name: "grok@grok-4.3",
  3. APIKey: os.Getenv("GROK_API_KEY"),
  4. })
  5. out, _, _ := agent.Send(ctx, []llmrouter.Message{
  6. {Role: "user", Content: "What did @xai post this week?"},
  7. }, tools, llmrouter.ReasoningDefault, llmrouter.ModeDefault)

tools is any non-empty function tool list, such as the get_weather example above; its presence is what attaches x_search.

中文