Skip to content

LangGraph middleware

Middleware adapts model requests before provider invocation. AI Hub currently uses it for:

  • tenant/user-aware dynamic prompts;
  • active-agent and tool filtering;
  • message-count and token-count history limits;
  • persisted conversation summarization;
  • cleanup of tool-call history and specialist-specific context.

Agents are created with langchain.agents.create_agent and receive middleware instances or decorated dynamic-prompt functions:

from langchain.agents import create_agent

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