Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions examples/src/createAgent/middleware/toolCallLimit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Basic example demonstrating tool call limit middleware.
*
* This middleware helps prevent infinite loops or excessive tool usage
* by limiting the number of tool calls an agent can make.
*/

import { z } from "zod";
import { createAgent, tool, toolCallLimitMiddleware } from "langchain";
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage } from "@langchain/core/messages";
import { MemorySaver } from "@langchain/langgraph";
const config = { configurable: { thread_id: "demo-thread" } };

/**
* Define a simple search tool
*/
const searchTool = tool(
async ({ query }) => {
console.log(`Searching for: ${query}`);
return `Results for: ${query}`;
},
{
name: "search",
description: "Search for information",
schema: z.object({
query: z.string(),
}),
}
);

/**
* Create an agent with a tool call limit
*/
const agent = createAgent({
model: new ChatOpenAI({ model: "gpt-4o-mini" }),
tools: [searchTool],
checkpointer: new MemorySaver(),
middleware: [
/**
* Limit to 3 tool calls per conversation
*/
toolCallLimitMiddleware({
threadLimit: 3,
/**
* Gracefully end when limit is reached
*/
exitBehavior: "end",
}),
],
});

/**
* Example conversation that would exceed the limit
*/
const result = await agent.invoke(
{
messages: [
new HumanMessage(
"Search for 'AI', 'ML', 'Deep Learning', 'Neural Networks', and 'LLMs'"
),
],
},
{ configurable: { thread_id: "demo-thread" } }
);

console.log("\nAgent response:");
console.log(result.messages[result.messages.length - 1].content);

/**
* Create an agent with a tool call limit
*/
const agent2 = createAgent({
model: new ChatOpenAI({ model: "gpt-4o-mini" }),
tools: [searchTool],
checkpointer: new MemorySaver(),
middleware: [
/**
* Limit to 3 tool calls per conversation
*/
toolCallLimitMiddleware({
threadLimit: 3,
/**
* Gracefully end when limit is reached
*/
exitBehavior: "end",
}),
],
});

const result2 = await agent.invoke(
{
messages: [new HumanMessage("Search for 'AI' and 'ML'")],
},
{ configurable: { thread_id: "demo-thread" } }
);

console.log("\nAgent response:");
console.log(result2.messages[result2.messages.length - 1].content);
5 changes: 5 additions & 0 deletions libs/langchain/src/agents/middlewareAgent/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export {
piiRedactionMiddleware,
type PIIRedactionMiddlewareConfig,
} from "./piiRedaction.js";
export {
toolCallLimitMiddleware,
ToolCallLimitExceededError,
type ToolCallLimitConfig,
} from "./toolCallLimit.js";
export {
modelCallLimitMiddleware,
type ModelCallLimitMiddlewareConfig,
Expand Down
Loading