Weather Tool Example

A complete working TypeScript tool loop for location-based weather answers.

Complete Example

TypeScript
import { TheRouter.ai } from "@therouter/sdk";

const client = new TheRouter.ai({
  apiKey: process.env.THEROUTER_API_KEY!,
  baseURL: "https://api.therouter.ai/v1",
});

const tools = [{
  type: "function",
  function: {
    name: "get_weather",
    description: "Current weather by city",
    parameters: {
      type: "object",
      properties: { city: { type: "string" } },
      required: ["city"],
    },
  },
}];

const weatherByCity: Record<string, string> = {
  london: "12C and cloudy",
  tokyo: "19C and clear",
};

const first = await client.callModel({
  model: "openai/gpt-4o-mini",
  input: [{ role: "user", content: "What is the weather in London?" }],
  tools,
});

const call = first.items.find((item) => item.type === "tool_call");
if (!call || call.type !== "tool_call") throw new Error("No tool call generated");

const city = String((call.arguments as { city?: string }).city || "").toLowerCase();
const output = weatherByCity[city] ?? "Data unavailable";

const second = await client.callModel({
  ...first.next_turn,
  input: [{ type: "tool_result", call_id: call.call_id, output }],
});

console.log(second.items);

Tool Contract

NameTypeRequiredDescription
function.name
stringRequiredTool identifier visible to model.
parameters
JSON SchemaRequiredArguments schema consumed by model.
call_id
stringRequiredReturned with each tool_call item.
tool_result.output
string | objectRequiredDeterministic tool response payload.

Sample Result

weather-result.json
{
  "output_text": "The weather in London is 12C and cloudy.",
  "stop_reason": "stop"
}
Production recommendation
Replace the in-memory map with a real weather API client and add retry plus timeout handling for network failures.