Skip to content

feat(adk): tool reduction middleware#746

Merged
N3kox merged 5 commits intoalpha/08from
feat/context_reduction_middleware
Feb 4, 2026
Merged

feat(adk): tool reduction middleware#746
N3kox merged 5 commits intoalpha/08from
feat/context_reduction_middleware

Conversation

@N3kox
Copy link
Contributor

@N3kox N3kox commented Feb 4, 2026

What type of PR is this?

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

重构 ADK 工具上下文压缩中间件(Tool Reduction Middleware)

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:

This PR introduces a tool context reduction middleware in ADK that supports per-tool configurable truncation of tool outputs (by max content length and max line length) for both invokable and streamable tools, and a token-based offload mechanism that, once the estimated tokens of conversation messages exceed a configurable threshold, offloads full tool results to a pluggable backend (with a default in-memory backend and /tmp file-path strategy) while replacing in-context content with a short reference string, along with utilities such as default tokenizer, default offload handler, and comprehensive unit tests covering truncation, offload, and default middleware construction behaviors.

zh(optional):

本 PR 在 ADK 中新增 context 压缩 middleware,支持按工具配置输出截断策略(包括按整体长度和单行长度截断),同时在对话消息估算 token 数超过可配置阈值时,对工具调用结果进行自动卸载 (offloading),将完整结果写入可插拔后端存储(默认使用内存文件系统与 "/tmp" 路径),并在上下文中以短提示文案替代原始内容;同时提供默认分词器、默认 offload 处理逻辑、默认压缩 middleware 配置,并补充了覆盖截断逻辑、offload 流程以及默认中间件构造的单元测试

(Optional) Which issue(s) this PR fixes:

(optional) The PR that updates user documentation:

@N3kox N3kox force-pushed the feat/context_reduction_middleware branch from d7660fe to 5609e6f Compare February 4, 2026 06:21
@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 61.21884% with 140 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (alpha/08@a74b18b). Learn more about missing BASE report.

Files with missing lines Patch % Lines
adk/middlewares/reduction/reduction.go 60.11% 105 Missing and 29 partials ⚠️
adk/middlewares/reduction/consts.go 75.00% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             alpha/08     #746   +/-   ##
===========================================
  Coverage            ?   80.04%           
===========================================
  Files               ?      145           
  Lines               ?    15266           
  Branches            ?        0           
===========================================
  Hits                ?    12220           
  Misses              ?     2083           
  Partials            ?      963           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@N3kox N3kox force-pushed the feat/context_reduction_middleware branch from 5609e6f to 7ac19a9 Compare February 4, 2026 06:28
@N3kox N3kox force-pushed the feat/context_reduction_middleware branch 2 times, most recently from c119c56 to b6ded0a Compare February 4, 2026 06:47
@N3kox N3kox force-pushed the feat/context_reduction_middleware branch from b6ded0a to 34aa0fc Compare February 4, 2026 08:14
@N3kox N3kox marked this pull request as ready for review February 4, 2026 08:15
@N3kox N3kox force-pushed the feat/context_reduction_middleware branch 2 times, most recently from b70f5fa to 419a3e8 Compare February 4, 2026 08:56
@N3kox N3kox force-pushed the feat/context_reduction_middleware branch from 419a3e8 to 8f1bba1 Compare February 4, 2026 09:26
ToolOffload: &ToolOffload{
TokenCounter: defaultTokenCounter,
ToolOffloadThreshold: &ToolOffloadThresholdConfig{
MaxTokens: 300000,
Copy link
Contributor

Choose a reason for hiding this comment

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

300k 是不是定太大了,现在的模型上下文窗口基本 200k 以下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

改成200k了

@N3kox N3kox force-pushed the feat/context_reduction_middleware branch from 8f1bba1 to afcef83 Compare February 4, 2026 10:28
@N3kox N3kox force-pushed the feat/context_reduction_middleware branch from 1e62bd2 to e52ae49 Compare February 4, 2026 11:39
@N3kox N3kox merged commit 2eeb557 into alpha/08 Feb 4, 2026
14 checks passed
@N3kox N3kox deleted the feat/context_reduction_middleware branch February 4, 2026 11:45
hi-pender pushed a commit that referenced this pull request Feb 14, 2026
* feat(adk): tool reduction middleware

* refactor(adk): move ancient reduction middleware to internal package

* chore(adk): reduction mw add i18n, rename TokenCounter

* chore(adk): add reduction mw comments

* chore(adk): refactor reduction mw config field, tool stream copy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants