Skip to content

Chat History Limits Middleware

Conversation history management for token optimization.

Configuration

  • File: src/chatbot/agentic/middlewares/limit_chat_history_middlewares.py

Available Middlewares

LimitChatHistoryMiddleware

Generic chat history limiter with configurable message count.

LimitOpportunitiesSuggestionChatHistoryMiddleware

Specialized for opportunity search conversations.

LimitOpportunitiesSuggestionToolsSelectionMiddleware

Limits tool selection history for opportunity agents.

LimitTalentFinderChatHistoryMiddleware

Specialized for talent finder conversations.

Purpose

  • Token optimization - Reduce context window usage
  • Performance - Faster LLM responses
  • Cost reduction - Fewer tokens = lower costs
  • Focus - Keep conversations relevant

Configuration Example

from langchain.agents import create_agent

from src.chatbot.agentic.middlewares.limit_chat_history_middlewares import (
    LimitChatHistoryMiddleware,
)

agent = create_agent(
    model=llm, tools=tools, middleware=[LimitChatHistoryMiddleware(max_messages=10)]
)

Best Practices

  • Use shorter history for focused tasks (search, disambiguation)
  • Keep longer history for complex conversations (career planning)

The common limiter applies both the configured message count and a default 12,000-token ceiling. The supervisor summarizes at 24,000 tokens and keeps 8,000 recent tokens; see the LangGraph architecture.