Skip to content

Commit dfb7f24

Browse files
Merge branch 'v1' into cb/context-editing-middleware
2 parents 8dabdaa + 3f75418 commit dfb7f24

File tree

4 files changed

+1187
-0
lines changed

4 files changed

+1187
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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);

libs/langchain/src/agents/middlewareAgent/middleware/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export {
3333
type ClearToolUsesEditConfig,
3434
type TokenCounter,
3535
} from "./contextEditing.js";
36+
export {
37+
toolCallLimitMiddleware,
38+
ToolCallLimitExceededError,
39+
type ToolCallLimitConfig,
40+
} from "./toolCallLimit.js";
3641
export {
3742
modelCallLimitMiddleware,
3843
type ModelCallLimitMiddlewareConfig,

0 commit comments

Comments
 (0)