Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.sudoapp.dev/llms.txt

Use this file to discover all available pages before exploring further.

Tool calling allows AI models to execute functions and interact with external systems, enabling more dynamic and powerful applications.

Basic Tool Calling

Simple Function Definition

Define a tool within your application that the AI model can call.
TypeScript
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 AI
const 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();

Streaming with Tools

TypeScript
import { Sudo } from "sudo-ai";

const sudo = new Sudo({
  serverURL: "https://sudoapp.dev/api",
  apiKey: process.env.SUDO_API_KEY ?? "",
});

async function streamWithToolCalls() {
  const tools = [
    {
      type: "function" as const,
      function: {
        name: "search_database",
        description: "Search for information in the database",
        parameters: {
          type: "object",
          properties: {
            query: { 
              type: "string", 
              description: "Search query" 
            }
          },
          required: ["query"]
        }
      }
    }
  ];
  
  try {
    const stream = await sudo.router.createStreaming({
      model: "gpt-4o",
      messages: [
        { role: "user", content: "Find information about TypeScript frameworks" }
      ],
      tools: tools,
      toolChoice: "auto"
    });
    
    const toolCalls: any[] = [];
    const contentParts: string[] = [];
    
    for await (const chunk of stream) {
      if (chunk.data?.choices) {
        for (const choice of chunk.data.choices) {
          if (choice.delta) {
            // Regular content
            if (choice.delta.content) {
              const content = choice.delta.content;
              contentParts.push(content);
              process.stdout.write(content);
            }
            
            // Tool calls
            if (choice.delta.toolCalls) {
              for (const toolCallDelta of choice.delta.toolCalls) {
                // Handle streaming tool calls
                console.log(`\n[Tool Call: ${toolCallDelta.function?.name}]`);
                if (toolCallDelta.function?.arguments) {
                  console.log(`[Args: ${toolCallDelta.function.arguments}]`);
                }
              }
            }
          }
        }
      }
    }
    
    console.log();
  } catch (error) {
    console.error("Streaming tool calling error:", error);
  }
}

streamWithToolCalls();

Next Steps

  1. Structured Output - Generate JSON and structured data
  2. Image Input - Work with multimodal inputs
  3. Reasoning - Advanced reasoning capabilities
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.