Created
January 24, 2026 19:05
-
-
Save capaj/d6399ff29b7a78a7c085a815dab2de81 to your computer and use it in GitHub Desktop.
ToolLoopAgent error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { google } from '@ai-sdk/google' | |
| import { ToolLoopAgent } from 'ai' | |
| import { z } from 'zod' | |
| const triggerSchema = z.object({ | |
| trigger: z.string().describe('any string to trigger') | |
| }) | |
| const tools = { | |
| flakyTool: { | |
| description: 'Very high chance it fails, but lets try it', | |
| parameters: triggerSchema, | |
| execute: async ({ trigger }: { trigger: string }) => { | |
| console.log('flakyTool called with:', trigger) | |
| throw new Error('nope') | |
| } | |
| }, | |
| fallbackTool: { | |
| description: 'Always works, unlike my first startup', | |
| parameters: triggerSchema, | |
| execute: async ({ trigger }: { trigger: string }) => { | |
| console.log('fallbackTool called with:', trigger) | |
| return 'fallback success' | |
| } | |
| } | |
| } | |
| const agentWithThrow = new ToolLoopAgent({ | |
| model: google('gemini-3-flash-preview'), | |
| instructions: | |
| 'You are a helpful assistant. When a tool fails, try using a fallback tool.', | |
| tools, | |
| maxSteps: 5 | |
| }) | |
| const resultWithThrow = await agentWithThrow.generate({ | |
| prompt: 'Try the flaky tool, if it fails then call the fallback tool' | |
| }) | |
| console.log('Result:', resultWithThrow.text) | |
| console.log('Steps:', resultWithThrow.steps.length) | |
| const toolsNoThrow = { | |
| flakyTool: { | |
| description: 'Very high chance it fails, but lets try it', | |
| parameters: triggerSchema, | |
| execute: async ({ trigger }: { trigger: string }) => { | |
| console.log('flakyTool called with:', trigger) | |
| return { success: false, message: 'flaky failure' } | |
| } | |
| }, | |
| fallbackTool: { | |
| description: 'Always works, unlike my first startup', | |
| parameters: triggerSchema, | |
| execute: async ({ trigger }: { trigger: string }) => { | |
| console.log('fallbackTool called with:', trigger) | |
| return 'fallback success' | |
| } | |
| } | |
| } | |
| const agentNoThrow = new ToolLoopAgent({ | |
| model: google('gemini-3-flash-preview'), | |
| instructions: | |
| 'You are a helpful assistant. When a tool returns success: false, try using a fallback tool.', | |
| tools: toolsNoThrow, | |
| maxSteps: 5 | |
| }) | |
| const resultNoThrow = await agentNoThrow.generate({ | |
| prompt: 'Try the flaky tool, if it fails then call the fallback tool' | |
| }) | |
| console.log('Result:', resultNoThrow.text) | |
| console.log('Steps:', resultNoThrow.steps.length) | |
| process.exit(0) | |
| /* | |
| flakyTool called with: undefined | |
| fallbackTool called with: undefined | |
| Result: The flaky tool failed, so I called the fallback tool, which succeeded. | |
| Steps: 3 | |
| flakyTool called with: undefined | |
| fallbackTool called with: undefined | |
| Result: The flaky tool failed as expected, so I have successfully called the fallback tool. | |
| Steps: 3 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment