feat(webhooks): split webhook usage tokens by cache vs non-cache (#1345)

Surface cache-read and cache-creation input tokens (plus the
prompt_tokens_include_cached_segments flag) in webhook usage responses,
so callers can distinguish cached from non-cached input tokens.

The data already flowed end-to-end via providers.Usage and
agent.RunResult.Usage; the two webhook envelope structs (webhookLLMUsage
for the sync LLM webhook, callbackUsage for the async callback payload)
copied only 3 of the fields, dropping the cache data. Add the 3 cache
fields (mirroring providers.Usage JSON tags exactly, all omitempty) and
populate them. The async callbackUsage mapping is extracted into a pure
newCallbackUsage helper for unit testing.

Additive and non-breaking: omitempty keeps responses without caching
byte-identical. No schema change.
This commit is contained in:
thotam authored and GitHub committed 2026-07-04 23:26:49 +07:00
1 parent fa7a85b339
commit f87cdaf616
5 files changed
+97 -23

No files matched your search

+13 -2
View File
@@ -266,12 +266,20 @@ Triggers an agent with an input prompt. Available in all editions.
"usage": {
"prompt_tokens": 150,
"completion_tokens": 200,
"total_tokens": 350
"total_tokens": 350,
"cache_read_input_tokens": 120,
"cache_creation_input_tokens": 30,
"prompt_tokens_include_cached_segments": true
},
"finish_reason": "stop"
}
```
> `cache_read_input_tokens` and `cache_creation_input_tokens` are present only when
> prompt caching was active (omitted otherwise). When
> `prompt_tokens_include_cached_segments` is `true`, `prompt_tokens` already counts
> the cached segments, so non-cached input = `prompt_tokens - cache_read_input_tokens`.
Sync mode times out after the configured deadline (default **600s**). On timeout: `504 Gateway Timeout` with `webhook.llm_timeout`.
#### Agent-run timeouts (configurable)
@@ -428,7 +436,10 @@ User-Agent: goclaw-webhook/1
"usage": {
"prompt_tokens": 150,
"completion_tokens": 200,
"total_tokens": 350
"total_tokens": 350,
"cache_read_input_tokens": 120,
"cache_creation_input_tokens": 30,
"prompt_tokens_include_cached_segments": true
},
"metadata": {},
"error": ""