Define a tool within your application that the AI model can call.
TypeScript
Copy
import { Sudo } from "sudo-ai";const sudo = new Sudo({ serverURL: "https://sudoapp.dev/api", apiKey: process.env.SUDO_API_KEY ?? "",});function getWeather(location: string): string { // This would normally call a real weather API return `The weather in ${location} is sunny and 75°F`;}// Define the tool for the AIconst tools = [ { type: "function" as const, function: { name: "get_weather", description: "Get current weather information for a location", parameters: { type: "object", properties: { location: { type: "string", description: "The city and state, e.g. San Francisco, CA" } }, required: ["location"] } } }];async function basicToolCalling() { try { const response = await sudo.router.create({ model: "gpt-4o", messages: [ { role: "user", content: "What's the weather like in New York?" } ], tools: tools, toolChoice: "auto" }); const message = response.choices[0].message; // Check if the model wants to call a function if (message.toolCalls) { for (const toolCall of message.toolCalls) { if (toolCall.function.name === "get_weather") { const args = JSON.parse(toolCall.function.arguments); const weatherResult = getWeather(args.location); console.log(`Weather result: ${weatherResult}`); } } } else { console.log(`Response: ${message.content}`); } } catch (error) { console.error("Tool calling error:", error); }}basicToolCalling();
Tool calling works best with models like GPT-4o, Claude Sonnet-4, and other function-calling capable models. Always validate tool inputs and handle errors gracefully. Both TypeScript and Python SDKs provide the same powerful tool calling capabilities with language-appropriate patterns.