langchain - 💡(How to fix) Fix feat(langchain): add custom message param to ToolCallLimitMiddleware

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

ToolCallLimitMiddleware blocks a tool call once a thread/run limit is hit and injects an error ToolMessage back to the model. That message is currently hardcoded , e.g. "Tool call limit exceeded. Do not call '<tool>' again." (or an all-tools variant). I'd like an optional message parameter so callers can customize the content of that error ToolMessage without subclassing the middleware. Scope would be intentionally narrow: the override applies only to the blocked-call ToolMessage (the model-facing signal) in the 'continue' and 'end' exit behaviors.

Fix Action

Fix / Workaround

Concrete example: in a human-handoff flow, once a conversation is handed to a human we want to tell the model something specific , e.g. "This conversation was already handed off to a human; tell the user to wait for an agent instead of calling 'handoff' again." Today the only ways to change that text are to subclass the middleware or monkeypatch the internal helper that builds the message , both brittle and not per-instance.

Code Example

limiter = ToolCallLimitMiddleware(
    tool_name="handoff",
    run_limit=1,
    message=(
        "This conversation was already handed off to a human. "
        "Tell the user to wait for an agent instead of calling 'handoff' again."
    ),
)
RAW_BUFFERClick to expand / collapse

Submission checklist

  • This is a feature request, not a bug report or usage question.
  • I added a clear and descriptive title that summarizes the feature request.
  • I used the GitHub search to find a similar feature request and didn't find it.
  • I checked the LangChain documentation and API reference to see if this feature already exists.
  • This is not related to the langchain-community package.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Feature Description

ToolCallLimitMiddleware blocks a tool call once a thread/run limit is hit and injects an error ToolMessage back to the model. That message is currently hardcoded , e.g. "Tool call limit exceeded. Do not call '<tool>' again." (or an all-tools variant).

I'd like an optional message parameter so callers can customize the content of that error ToolMessage without subclassing the middleware. Scope would be intentionally narrow: the override applies only to the blocked-call ToolMessage (the model-facing signal) in the 'continue' and 'end' exit behaviors.

The 'end' summary AIMessage and the ToolCallLimitExceededError would keep their thread/run diagnostic detail. The default (message=None) stays exactly as it is today.

Use Case

The blocked-call ToolMessage is the model's only signal about what happened, and it drives what the model does next. A fixed "do not call X again" string works generically but can't carry domain context.

Concrete example: in a human-handoff flow, once a conversation is handed to a human we want to tell the model something specific , e.g. "This conversation was already handed off to a human; tell the user to wait for an agent instead of calling 'handoff' again." Today the only ways to change that text are to subclass the middleware or monkeypatch the internal helper that builds the message , both brittle and not per-instance.

Proposed Solution

Add message: str | None = None (keyword-only) to ToolCallLimitMiddleware.__init__. When set, it replaces the auto-generated ToolMessage content for blocked calls; when None, behavior is unchanged.

limiter = ToolCallLimitMiddleware(
    tool_name="handoff",
    run_limit=1,
    message=(
        "This conversation was already handed off to a human. "
        "Tell the user to wait for an agent instead of calling 'handoff' again."
    ),
)

Alternatives Considered

  1. Subclassing ToolCallLimitMiddleware and overriding after_model , duplicates non-trivial limit/counting logic and is fragile across releases.

Additional Context

Affects langchain.agents.middleware.tool_call_limit.ToolCallLimitMiddleware (langchain 1.3.x).

From my end a GitHub search didn't surface an existing request for a customizable blocked-call message. Happy to adjust scope or naming per maintainer preference.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING