mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
8369fa88fd
Two new columns added to aibridge_token_usages: - cache_read_input_tokens (BIGINT, default 0) - cache_write_input_tokens (BIGINT, default 0) Migration backfills existing rows by extracting values from the metadata JSONB column (cache_read_input, input_cached, prompt_cached for reads (max value selected since only 1 should be set), cache_creation_input for writes). All references to data from metadata were updated to reference new columns. No other changes then changing where data is extracted from. Requires aibridge library version bump to include: https://github.com/coder/aibridge/pull/229 Fixes: https://github.com/coder/aibridge/issues/150
27 lines
1.1 KiB
SQL
27 lines
1.1 KiB
SQL
ALTER TABLE aibridge_token_usages
|
|
ADD COLUMN cache_read_input_tokens BIGINT NOT NULL DEFAULT 0,
|
|
ADD COLUMN cache_write_input_tokens BIGINT NOT NULL DEFAULT 0;
|
|
|
|
-- Backfill from metadata JSONB. Old rows stored cache tokens under
|
|
-- provider-specific keys; new rows use the dedicated columns above.
|
|
UPDATE aibridge_token_usages
|
|
SET
|
|
|
|
-- Cache-read metadata keys by provider:
|
|
-- Anthropic (/v1/messages): "cache_read_input"
|
|
-- OpenAI (/v1/responses): "input_cached"
|
|
-- OpenAI (/v1/chat/completions): "prompt_cached"
|
|
cache_read_input_tokens = GREATEST(
|
|
COALESCE((metadata->>'cache_read_input')::bigint, 0),
|
|
COALESCE((metadata->>'input_cached')::bigint, 0),
|
|
COALESCE((metadata->>'prompt_cached')::bigint, 0)
|
|
),
|
|
|
|
-- Cache-write metadata keys by provider:
|
|
-- Anthropic (/v1/messages): "cache_creation_input"
|
|
-- OpenAI does not report cache-write tokens.
|
|
cache_write_input_tokens = COALESCE((metadata->>'cache_creation_input')::bigint, 0)
|
|
WHERE metadata IS NOT NULL
|
|
AND cache_read_input_tokens = 0
|
|
AND cache_write_input_tokens = 0;
|