function getTime() { return new Date().toLocaleTimeString();}
Important: The return value from your function is sent directly to the AI model, which uses it to generate its response to the user.Always return a value - even for action-based tools:
Data tools (weather, calculations): Return the actual data
Action tools (generate image, open browser): Return acknowledgment like “Image generated successfully” or “Browser tab opened”
On failure: Return error description like “Failed to generate image: rate limit error”
This tells the AI whether your tool succeeded or failed. See the implementation section for more details.
Always return meaningful values - the AI uses your return value to respond to the user:
Copy
Ask AI
// ✅ Good: Return actual datafunction getWeather({ city }: { city: string }) { return "72°F and sunny in San Francisco";}// ✅ Good: Return success confirmationfunction sendEmail({ to, subject }: { to: string; subject: string }) { // ... send email logic return `Email sent to ${to}`;}// ✅ Good: Return error detailsfunction uploadFile({ filename }: { filename: string }) { try { // ... upload logic return "File uploaded successfully"; } catch (error) { return `Upload failed: ${error.message}`; }}// ❌ Bad: Don't return undefined/nullfunction badTool() { // The AI gets nothing to work with return null;}
Handle errors gracefully:
Copy
Ask AI
function robustTool({ param }: { param: string }) { try { if (!param?.trim()) { return "Parameter is required"; } const result = performOperation(param); // notice that we're returning something that model can use to respond to the user return result || "Operation completed but no data returned"; } catch (error) { return `Error: ${error.message}`; // for the model to understand the error }}
Use async/await for API calls:
Copy
Ask AI
async function fetchData({ query }: { query: string }) { try { const response = await fetch(`/api/search?q=${query}`); const data = await response.json(); return `Found ${data.results.length} results for "${query}"`; } catch (error) { return "Search service unavailable"; // again, for the model to understand what went wrong }}