|
| 1 | +/** |
| 2 | + * Basic example demonstrating tool call limit middleware. |
| 3 | + * |
| 4 | + * This middleware helps prevent infinite loops or excessive tool usage |
| 5 | + * by limiting the number of tool calls an agent can make. |
| 6 | + */ |
| 7 | + |
| 8 | +import { z } from "zod"; |
| 9 | +import { createAgent, tool, toolCallLimitMiddleware } from "langchain"; |
| 10 | +import { ChatOpenAI } from "@langchain/openai"; |
| 11 | +import { HumanMessage } from "@langchain/core/messages"; |
| 12 | +import { MemorySaver } from "@langchain/langgraph"; |
| 13 | +const config = { configurable: { thread_id: "demo-thread" } }; |
| 14 | + |
| 15 | +/** |
| 16 | + * Define a simple search tool |
| 17 | + */ |
| 18 | +const searchTool = tool( |
| 19 | + async ({ query }) => { |
| 20 | + console.log(`Searching for: ${query}`); |
| 21 | + return `Results for: ${query}`; |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "search", |
| 25 | + description: "Search for information", |
| 26 | + schema: z.object({ |
| 27 | + query: z.string(), |
| 28 | + }), |
| 29 | + } |
| 30 | +); |
| 31 | + |
| 32 | +/** |
| 33 | + * Create an agent with a tool call limit |
| 34 | + */ |
| 35 | +const agent = createAgent({ |
| 36 | + model: new ChatOpenAI({ model: "gpt-4o-mini" }), |
| 37 | + tools: [searchTool], |
| 38 | + checkpointer: new MemorySaver(), |
| 39 | + middleware: [ |
| 40 | + /** |
| 41 | + * Limit to 3 tool calls per conversation |
| 42 | + */ |
| 43 | + toolCallLimitMiddleware({ |
| 44 | + threadLimit: 3, |
| 45 | + /** |
| 46 | + * Gracefully end when limit is reached |
| 47 | + */ |
| 48 | + exitBehavior: "end", |
| 49 | + }), |
| 50 | + ], |
| 51 | +}); |
| 52 | + |
| 53 | +/** |
| 54 | + * Example conversation that would exceed the limit |
| 55 | + */ |
| 56 | +const result = await agent.invoke( |
| 57 | + { |
| 58 | + messages: [ |
| 59 | + new HumanMessage( |
| 60 | + "Search for 'AI', 'ML', 'Deep Learning', 'Neural Networks', and 'LLMs'" |
| 61 | + ), |
| 62 | + ], |
| 63 | + }, |
| 64 | + { configurable: { thread_id: "demo-thread" } } |
| 65 | +); |
| 66 | + |
| 67 | +console.log("\nAgent response:"); |
| 68 | +console.log(result.messages[result.messages.length - 1].content); |
| 69 | + |
| 70 | +/** |
| 71 | + * Create an agent with a tool call limit |
| 72 | + */ |
| 73 | +const agent2 = createAgent({ |
| 74 | + model: new ChatOpenAI({ model: "gpt-4o-mini" }), |
| 75 | + tools: [searchTool], |
| 76 | + checkpointer: new MemorySaver(), |
| 77 | + middleware: [ |
| 78 | + /** |
| 79 | + * Limit to 3 tool calls per conversation |
| 80 | + */ |
| 81 | + toolCallLimitMiddleware({ |
| 82 | + threadLimit: 3, |
| 83 | + /** |
| 84 | + * Gracefully end when limit is reached |
| 85 | + */ |
| 86 | + exitBehavior: "end", |
| 87 | + }), |
| 88 | + ], |
| 89 | +}); |
| 90 | + |
| 91 | +const result2 = await agent.invoke( |
| 92 | + { |
| 93 | + messages: [new HumanMessage("Search for 'AI' and 'ML'")], |
| 94 | + }, |
| 95 | + { configurable: { thread_id: "demo-thread" } } |
| 96 | +); |
| 97 | + |
| 98 | +console.log("\nAgent response:"); |
| 99 | +console.log(result2.messages[result2.messages.length - 1].content); |
0 commit comments