Files
myWiki/concepts/stub-pattern.md

1.3 KiB
Raw Blame History

title, created, updated, type, tags, sources
title created updated type tags sources
Stub Pattern轻量化桩模式 2026-05-11 2026-05-11 concept
agent-architecture
tool-design
caching-optimization
prompt-caching-architecture

Stub Pattern轻量化桩模式

定义

Stub Pattern 是一种解决 prompt-caching 中工具定义变更导致缓存失效的架构模式。在 System Prompt 中预先定义所有潜在工具的轻量占位符Stubs仅包含工具名称和最小化接口描述运行时通过 tool-registry 统一接口间接调用。

问题

"按需加载工具"——根据任务动态添加或移除 System Prompt 中的工具定义——是缓存性能的"毒药"。每次工具定义变更都导致整个缓存前缀失效。

解决方案

  1. System Prompt 中固定所有工具的 Stubfetch_factor_data(ticker, time_range)
  2. 模型不直接调用 Stub而是调用 ToolRegistry.invoke(tool_id, params)
  3. ToolRegistry 后台负责动态加载和执行具体逻辑
  4. 前缀序列始终保持稳定 → cache-hit-ratio 维持在 99%+

附加收益

降低 Agent 面对复杂任务时的决策压力(无需在大量工具中动态选择)。

相关概念