flaky-scooter-85113
09/23/2025, 6:23 PMtall-airline-16756
09/23/2025, 6:23 PMtall-airline-16756
09/23/2025, 6:24 PMexecute method using valid arguments that match the tool's defined schema, without needing to rely on LLM-triggered execution.
Here’s a friendly example to guide you:
import { z } from 'zod';
import { llm } from '@livekit/agents';
// Define a tool to get the weather
const getWeather = llm.tool({
description: 'Get the weather for a given location',
parameters: z.object({
location: z.string().describe('The city and state, e.g. San Francisco, CA'),
unit: z.enum(['celsius', 'fahrenheit']).describe('The temperature unit to use'),
}),
execute: async ({ location, unit }, { ctx }) => {
// Simulate external service call
return `The weather in ${location} is 25 degrees ${unit}.`;
},
});
// Manually execute the tool with known parameters
const result = await getWeather.execute(
{ location: 'Paris', unit: 'celsius' },
{ ctx: { userData: {} }, toolCallId: 'test-call-123' }
);
console.log(result); // Output: "The weather in Paris is 25 degrees celsius."
To make sure everything runs smoothly, here are a couple of important points to keep in mind:
• Schema Validation: Make sure the arguments you provide match the tool's parameter schema exactly. If something’s off, a validation error will let you know.
• ToolOptions Context: The second argument to execute should be a ToolOptions object, which must include ctx (a RunContext), and can optionally have toolCallId and abortSignal. Using the createToolOptions helper can make this easier.
This method is fully supported and dependable in agents-js v1.0.5, so you can confidently use it in your projects. If you have any more questions or need further assistance, I’m here to help!
Sources: README.md