awslabs / awslabs/agentcore-samples

LangGraph ReAct agent context window exploding despite ContextEditingMiddleware - need help

Open
#770 0 comments 0 reactions 1 assignee Claimed by @EashanKaushik View on GitHub
help wanted
Dominant language
Python
Stars
3.4k
Forks
1.3k
Avg merge
1d 22h
Merged PRs (30d)
30

Description

## Summary
`ClearToolUsesEdit` middleware does not trim context as expected - agent context keeps growing despite configuration, leading to throttling and performance issues.

## Details

I'm running a LangGraph ReAct agent with multiple tool calls per user query (Knowledge Base searches + SQL executions). Despite configuring `ClearToolUsesEdit`, the context continues to grow to 150K-200K tokens.

**Environment:**
- `langgraph` version: 0.3.x (latest)
- `langchain-aws` version: latest
- Python 3.11
- Running on AWS Bedrock AgentCore Runtime (serverless)
- Model: Claude Haiku (200K context limit)
- Using Bedrock AgenCore Memory (Short term)

**Current Configuration:**

from langgraph.context_editing import ContextEditingMiddleware, ClearToolUsesEdit

context_editor = ContextEditingMiddleware(
edits=[ClearToolUsesEdit(
trigger=100000, # Trigger at 100K tokens
clear_at_least=20000, # Reclaim at least 20K tokens
keep=5, # Keep 5 most recent tool results
clear_tool_inputs=True,
)]
)

agent = create_react_agent(
model=llm,
tools=tools,
prompt=system_prompt,
context_editing=context_editor,
)

**What I've Tried:**
1. Lowering `trigger` from 100K to 50K to 10K
2. Increasing `clear_at_least` to 20K, 30K
3. Reducing `keep` from 5 to 3 to 1
4. Setting `clear_tool_inputs=True`
5. Various combinations of the above

**Observed Behavior:**
- Context grows linearly with each tool call
- By the 5th-6th tool call, context reaches 100K+ tokens
- Agent gets throttled by Bedrock TPM limits
- Trimming doesn't seem to occur, or occurs too late

Questions:

When does trimming actually happen - before or after LLM call?

Does trigger mean "trim when context exceeds this" or something else?

Better alternatives for aggressive context management?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.