// Start from the user-only input, not the input left over from the Client Tools walkthrough.
input = []any{
map[string]any{
"role": "user",
"content": "What are the titles of some James Joyce books?",
},
}
// Runs each function_call, then sends one non-streaming follow-up request with the results
handleToolCalls := func(toolCalls []map[string]any, outputItems []any) {
followUp := append(append([]any{}, input...), outputItems...)
for _, call := range toolCalls {
followUp = append(followUp, map[string]any{
"type": "function_call_output",
"call_id": call["call_id"],
"output": runSearch(call["arguments"].(string)),
})
}
body, _ := json.Marshal(map[string]any{
"model": "anthropic/claude-sonnet-4.5",
"input": followUp,
"tools": tools,
})
req, _ := http.NewRequest("POST", "https://openrouter.ai/api/v1/responses", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("OPENROUTER_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var result struct {
Output []struct {
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
} `json:"output"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
log.Fatal(err)
}
for _, item := range result.Output {
for _, part := range item.Content {
if part.Type == "output_text" {
fmt.Println(part.Text)
}
}
}
}
body, _ := json.Marshal(map[string]any{
"model": "anthropic/claude-sonnet-4.5",
"input": input,
"tools": tools,
"stream": true,
})
req, _ := http.NewRequest("POST", "https://openrouter.ai/api/v1/responses", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("OPENROUTER_API_KEY"))
req.Header.Set("Content-Type", "application/json")
stream, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer stream.Body.Close()
if stream.StatusCode != http.StatusOK {
log.Fatalf("Expected a streaming response, got %s", stream.Status)
}
var toolCalls []map[string]any
var outputItems []any
scanner := bufio.NewScanner(stream.Body)
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimSpace(line[6:])
if data == "[DONE]" {
continue
}
var event map[string]any
if err := json.Unmarshal([]byte(data), &event); err != nil {
log.Fatal(err)
}
switch event["type"] {
case "response.failed":
log.Fatalf("Response failed: %v", event["response"].(map[string]any)["error"])
case "response.incomplete":
log.Fatalf("Response incomplete: %v", event["response"].(map[string]any)["incomplete_details"])
case "response.output_text.delta":
fmt.Print(event["delta"])
case "response.output_item.done":
// Preserve every completed item in order. Reasoning and assistant items are
// required context for some providers when sending tool results.
item := event["item"].(map[string]any)
outputItems = append(outputItems, item)
if item["type"] == "function_call" {
// A completed function_call item carries the full arguments string.
toolCalls = append(toolCalls, item)
}
case "response.completed":
fmt.Println()
if len(toolCalls) > 0 {
// Run each function_call locally, then send a follow-up request whose input
// includes the original input, every output item, and a function_call_output
// for each call (see A Simple Agentic Loop on Client Tools).
handleToolCalls(toolCalls, outputItems)
}
// Otherwise the regular response was printed from its output_text deltas above
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}