Menu ▾ ▴

#68 LLMSlimToolOutputMiddleware: cache-safe tool-output shortening for LangChain agents

closed
nobody
None
2026-10-18
2026-10-17
Anonymous
No

Originally created by: Thanatos9404

Builds on [#67] (llmslim wrap claude); merge that first. It shares the shortening core moved into llmslim/_tool_output.py.

What

LLMSlimToolOutputMiddleware is middleware for LangChain 1.x create_agent. It needs pip install "llmslim[langchain-agents]" (Python 3.10+).

agent = create_agent(
    "anthropic:claude-haiku-5-5",
    tools=[run_tests, search_logs, read_file],
    middleware=[LLMSlimToolOutputMiddleware(never={"read_file"})],
)
  • Which results: in before_model it takes tool results the model hasn't seen yet (those after the last AIMessage) that are longer than min_tokens.
  • What it keeps: errors, failures and tracebacks first, then the lines related to the call. Similar lines are collapsed and a note is added. No model is called.
  • Where it goes: the shorter text is written back into the agent's state under the same message id, and LangGraph's reducer replaces the original. Every later model call sends the same bytes.

Why it matters

LangChain's built-in ContextEditingMiddleware replaces old tool results with [cleared]. That throws the content away, and it changes the prompt prefix each time it clears, so provider prompt caches miss. Here nothing the model has already seen ever changes:

  • caches keep hitting;
  • Claude's preserved thinking stays valid.

Evidence

  • Tests in a real create_agent, with a scripted chat model that records what it is sent:
  • first-sight shortening;
  • persistence under the same id;
  • the next turn on the same thread sends identical bytes, with an unchanged prefix;
  • only/never filters;
  • image results left alone;
  • already-seen results never rewritten;
  • the async path.
  • Live, with langchain-anthropic 1.7.6, Claude Haiku 5.5 and adaptive thinking:
  • a 7,545-token pytest log reached the model as 160 tokens;
  • the answer named the failing test and its assertion;
  • a second turn with thinking in the history answered correctly, with no error.
  • Checks: mypy is clean against real LangChain types, and ruff is clean. The full suite passes: 1,811 passed. The LangChain tests run where the extra is installed.

The module is in the coverage omit list like the other framework adapters. The framework-free shortening in _tool_output.py is measured everywhere.

Related

Tickets: #22
Tickets: #67
Tickets: #69

Discussion

  • Anonymous

    Anonymous - 2026-10-17

    Originally posted by: vercel[bot]

    The latest updates on your projects. Learn more about Vercel for GitHub.

    Project Deployment Actions Updated
    llmslim Ready Ready Preview <relative-time datetime="2026-10-17T10:11:56.599Z">Oct 17, 2026 10:11am UTC</relative-time>
     
  • Anonymous

    Anonymous - 2026-10-17

    Ticket changed by: Thanatos9404

    • status: open --> closed
     

Log in to post a comment.