WeKnora/internal/models/chat
ochan.kwon 894cbec48a feat(agent): report and attribute per-turn LLM token usage
Accumulate token usage across every LLM round of an agent turn and
expose the aggregate everywhere a turn is observable:

- AgentState.TurnUsage accumulates each round's reported usage,
  including the final-answer synthesis call; TokenUsage.Accumulate
  keeps the cache counters as prompt subsets and recomputes the
  combined cache status (unsupported survives only while every call
  was itself unsupported).
- The agent completion event carries the aggregate, and the complete
  stream event exposes it both as data.usage and as a typed
  StreamEvent.Usage that buildStreamResponse promotes into the
  previously-unused StreamResponse.Usage wire field.
- The assistant message persists it as a nullable usage column on
  both migration targets (postgres 000085 jsonb, sqlite 000012 text),
  for web and IM channels alike, so history reads still carry the
  turn's cost after the live stream is gone.
- "[LLM Usage]" log lines gain ", session_id=..., principal=type:id"
  when the context carries them; calls outside sessions stay
  byte-identical.

Usage from failed retry attempts is not accumulated: the per-call
usage log remains the ground truth, while the turn aggregate serves
as the operational signal.
2026-08-24 10:36:19 +08:00
..
anthropic.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
anthropic_test.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
chat.go feat(models): add max concurrency setting for background tasks 2026-07-09 20:40:51 +08:00
concurrency_wrapper.go feat(system): expose model concurrency stats on runtime queues dashboard 2026-07-11 07:17:28 +08:00
concurrency_wrapper_test.go fix(models): harden background concurrency governor 2026-07-09 20:40:51 +08:00
config_from_model_test.go feat(models): support custom HTTP headers across all remote model calls 2026-04-23 23:36:40 +08:00
image_resolve.go feat(resource): add stable resource registry with LLM-context alias compaction 2026-07-15 17:02:43 +08:00
json_field_extractor.go feat: implement final_answer tool and enhance agent duration tracking 2026-03-09 11:46:56 +08:00
json_field_extractor_test.go feat: implement final_answer tool and enhance agent duration tracking 2026-03-09 11:46:56 +08:00
langfuse_wrapper.go refactor(agent): introduce modelcontext registry and fix handle-codec bugs 2026-07-27 11:05:43 +08:00
langfuse_wrapper_test.go refactor(agent): introduce modelcontext registry and fix handle-codec bugs 2026-07-27 11:05:43 +08:00
llm_debug.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
llm_debug_wrapper.go feat(logger): implement LLM debug logging functionality 2026-04-16 23:15:02 +08:00
ollama.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
ollama_test.go fix(security): harden outbound requests and file previews 2026-07-02 19:15:46 +08:00
openai_request.go fix(chat): preserve provider tool-call metadata 2026-06-09 20:55:20 +08:00
openai_stream.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
prompt_cache.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
provider.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
provider_test.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
remote_api.go fix(security): harden outbound URL SSRF protection (#2626) 2026-08-10 13:24:14 +08:00
remote_api_test.go feat(llm): add prompt-cache observability and wiki cache-friendly prompts 2026-07-23 19:17:30 +08:00
sse_reader.go fix: ReadEvent incompatible sse data format 2026-04-20 22:29:38 +08:00
stream_emit.go feat(thinkingControl): implement thinking mode configuration in model editor 2026-06-05 14:56:48 +08:00
stream_raw_dump.go refactor(chat): improve model name sanitization and path validation 2026-06-05 19:35:02 +08:00
thinking.go feat(models): add model test debugger for saved model configs 2026-06-25 17:05:33 +08:00
thinking_test.go feat(models): add model test debugger for saved model configs 2026-06-25 17:05:33 +08:00
timeout_test.go fix(chat): LLM 兜底超时仅在上层无 deadline 时生效,并修复 ChatStream raw HTTP 路径的 cancel 泄漏 2026-05-10 00:16:12 +08:00
transport.go fix(security): harden outbound URL SSRF protection (#2626) 2026-08-10 13:24:14 +08:00
usage.go feat(agent): report and attribute per-turn LLM token usage 2026-08-24 10:36:19 +08:00
usage_attribution_test.go feat(agent): report and attribute per-turn LLM token usage 2026-08-24 10:36:19 +08:00