Skip to content

Conversation

nfcampos
Copy link
Collaborator

@nfcampos nfcampos commented Oct 4, 2025

which implements a tool call budget for either all tools, or a specific tool

This defaults to __class__.__name__ as before, but can be overriden to support using
multiple instances of same middleware in same agent
@github-actions github-actions bot added langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0 labels Oct 4, 2025
@nfcampos nfcampos changed the title Add ToolCallLimitMiddleware feat(langchain_v1): Add ToolCallLimitMiddleware Oct 4, 2025
@nfcampos nfcampos changed the title feat(langchain_v1): Add ToolCallLimitMiddleware feat(langchain_v1) Add ToolCallLimitMiddleware Oct 4, 2025
@nfcampos nfcampos changed the title feat(langchain_v1) Add ToolCallLimitMiddleware feat(langchain_v1): Add ToolCallLimitMiddleware Oct 4, 2025
@eyurtsev eyurtsev self-assigned this Oct 4, 2025
class ToolCallLimitMiddleware(AgentMiddleware):
"""Middleware that tracks tool call counts and enforces limits.

This middleware monitors the number of tool calls made during agent execution
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

--

Thoughts on integration with SummarizationMiddleware which will be removing messages from chat history? Should the middleware extend the state to keep track of tool calls? Or do we just document?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just document

Copy link
Collaborator

@eyurtsev eyurtsev Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious if you have any specific opposition to tracking in state? (or do you think it's too much complexity for this middleware?)

@eyurtsev eyurtsev merged commit 2286d0d into master Oct 4, 2025
36 checks passed
@eyurtsev eyurtsev deleted the nc/4oct/tool-call-limit-middleware branch October 4, 2025 19:03
@sydney-runkle
Copy link
Collaborator

Looks good as an initial pass!

I feel like if you have 10 tools and you want different limits for each of them it might be a bit annoying to have 10 middlewares (a map would be nicer), but that seems like a pretty out there use case.

@christian-bromann
Copy link
Member

What do we do if the model decides to call x tools at once but we set the limit n to be n < x? We could have an additional check in the afterModel hook and throw before any tool calls, thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants