Skip to content

feat(langchain): expose toggle to trace inputs on AgentMiddleware#38910

Open
ccurme (ccurme) wants to merge 11 commits into
masterfrom
cc/profiling-test-1
Open

feat(langchain): expose toggle to trace inputs on AgentMiddleware#38910
ccurme (ccurme) wants to merge 11 commits into
masterfrom
cc/profiling-test-1

Conversation

@ccurme

@ccurme ccurme (ccurme) commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

Requires langchain-ai/langgraph#8362

AgentMiddleware traces state messages in its input (once in langgraph state for before/after_* node-type hooks, and in both request.messages and langgraph state for wrap-type hooks). For longish message histories this adds considerable latency: we've observed 500 - 1,000ms per middleware in deepagents' eval suite. For the default middleware stack this can dwarf the latency from the actual LLM call.

Here we add a tracing config to AgentMiddleware to allow inputs from middleware traces to be completely scrubbed:

class TraceConfig(TypedDict, total=False):

    inputs: bool

~70% of the latency is from wrap-type hooks. We already choose the inputs to those hooks via traceable, scrubbing runtime and handler. Here we let you trace no inputs from those hooks.

20% of the latency is from node-type hooks. For this we expose the trace_inputs callable of the underlying node's RunnableSeq through to add_node (done in langchain-ai/langgraph#8362), and access it here in langchain.

We could alternatively selectively scrub keys from state (like messages). But the solution here is cleaner and simpler, and also will work for files in StateBackend or other large data.

We don't expose a toggle for tracing outputs because those are in practice small (typically a single message in a ModelResponse or state update).

Importantly, we flip tracing inputs on AgentMiddleware to False, which would loses any meaningful inputs from trace data. If we're concerned about this, we can flip the default and update built-in middleware in deepagents. At any rate, users can opt-in by passing tracing={"inputs": True} to middleware.

Before:
before

After:
after

@github-actions github-actions Bot added dependencies Pull requests that update a dependency file (e.g. `pyproject.toml` or `uv.lock`) feature For PRs that implement a new feature; NOT A FEATURE REQUEST internal langchain `langchain` package issues & PRs size: S 50-199 LOC labels Jul 17, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 17, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 2 untouched benchmarks
⏩ 15 skipped benchmarks1


Comparing cc/profiling-test-1 (3800498) with master (51db348)2

Open in CodSpeed

Footnotes

  1. 15 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on master (339fcf3) during the generation of this report, so 51db348 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@open-swe open-swe Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Open SWE Review found 1 potential issue.

Open in WebView Open SWE trace

Comment thread libs/langchain_v1/pyproject.toml Outdated
langchain-openai = { path = "../partners/openai", editable = true }
langchain-anthropic = { path = "../partners/anthropic", editable = true }
# TEMPORARY: pull langgraph from the cc/profiling-test-1 branch (adds add_node trace_inputs).
langgraph = { git = "https://github.com/langchain-ai/langgraph", subdirectory = "libs/langgraph", branch = "cc/profiling-test-1" }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟠 Published package can install incompatible langgraph

This source override only affects uv installs from the repo; it does not change the wheel metadata, which still advertises langgraph>=1.2.5,<1.3.0. The code in this PR now calls StateGraph.add_node(..., trace_inputs=...), but the PR notes that kwarg comes from the temporary langgraph branch. A user installing langchain with an already-locked/allowed langgraph==1.2.5 can therefore construct an agent with a before/after middleware hook and get TypeError: add_node() got an unexpected keyword argument 'trace_inputs'. Please depend on a released langgraph version that contains add_node(trace_inputs=...) and bump the lower bound accordingly, rather than relying on the local source override.

(Refers to line 105)


Your feedback helps Open SWE learn. React with 👍 or 👎 to tell us if this review comment was useful.

@open-swe open-swe Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Open SWE Review found 1 potential issue.

Open in WebView Open SWE trace

tools: Sequence[BaseTool]
"""Additional tools registered by the middleware."""

trace_inputs: bool = False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Middleware traces drop inputs by default

Changing the exported AgentMiddleware.trace_inputs default to False changes behavior for every existing middleware subclass and decorator-created middleware that does not explicitly set this attribute: after upgrading, their hook spans will stop recording the request/state inputs in LangSmith traces. Since this PR is adding an opt-out toggle, the public default should preserve the existing trace payloads (True) and let latency-sensitive callers opt into scrubbing by setting trace_inputs=False.

(Refers to line 401)


Your feedback helps Open SWE learn. React with 👍 or 👎 to tell us if this review comment was useful.

Suggested change
trace_inputs: bool = False
trace_inputs: bool = True

@github-actions github-actions Bot added size: M 200-499 LOC and removed size: S 50-199 LOC labels Jul 21, 2026
@github-actions github-actions Bot added size: S 50-199 LOC and removed size: M 200-499 LOC labels Jul 21, 2026
ccurme (ccurme) and others added 2 commits July 22, 2026 12:26
The cc/profiling-test-1 langgraph branch no longer exists; repoint to the
revert branch so downstream locks (deepagents) can resolve.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file (e.g. `pyproject.toml` or `uv.lock`) feature For PRs that implement a new feature; NOT A FEATURE REQUEST internal langchain `langchain` package issues & PRs size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant