| name | tool-middleware |
| description | Configure and extend the tool middleware chain (retry, timeout, rate-limit, cache) in Superagent. TRIGGER when: adding retry logic to a tool, configuring per-tool timeouts, implementing tool response caching, debugging "tool timeout" errors, or when the user asks "how do I add retry to a tool", "工具超时怎么设置", "tool middleware". DO NOT TRIGGER when: implementing the tool's core logic or writing MCP servers.
|
| origin | learned |
| tags | ["tool","middleware","retry","timeout","cache","rate-limit"] |
Tool Middleware
Source: backend/pkg/tool/. Every tool call passes through a configurable middleware chain.
Default Chain Order
ContextCache → RateLimit → Timeout → Retry → [Tool Function]
Agent YAML Configuration
spec:
tool_config:
defaults:
timeout_ms: 10000
retry:
max_attempts: 3
backoff: exponential
initial_delay_ms: 200
per_tool:
mcp://brave-search/search:
timeout_ms: 5000
retry:
max_attempts: 2
builtin/calculator:
cache:
ttl_seconds: 0
Middleware Reference
Retry
retry:
max_attempts: 3
backoff: exponential
initial_delay_ms: 200
max_delay_ms: 5000
retryable_errors:
- timeout
- rate_limit
Timeout
timeout_ms: 10000
Rate Limit
rate_limit:
requests_per_minute: 60
burst: 10
Context Cache
Caches tool results keyed by (tool_name, input_hash) within a single agent turn.
Prevents duplicate calls when the LLM re-calls the same tool with identical input.
cache:
ttl_seconds: 300
max_size_kb: 512
Programmatic Middleware (Go)
mgr := tool.NewManager()
mgr.Use(tool.TimeoutMiddleware(10 * time.Second))
mgr.Use(tool.RetryMiddleware(tool.RetryConfig{MaxAttempts: 3}))
mgr.Use(myCustomMiddleware)
type Middleware func(next InvokeFunc) InvokeFunc
type InvokeFunc func(ctx context.Context, name string, input map[string]any) (map[string]any, error)
Debugging
APP_LOG_LEVEL=debug make dev-server | grep "tool_middleware"
grep "tool_retry" logs/app.log | wc -l
Common issues:
tool timeout with low timeout_ms → increase or set per-tool override.
- Duplicate LLM tool calls → enable
cache with short TTL.
- Rate limit errors from external APIs → configure
rate_limit below provider limits.