Requests can be tagged: metadata, x-llmgw-tags and the user field on the usage record, and counted by the tags router.tag_labels names

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 19:45:37 +02:00
1 parent e59ff52932
commit 29a7347df2
19 files changed
+436 -17

No files matched your search

@@ -0,0 +1,7 @@
-- Ported from the SQLite migration of the same number.
-- What a client said about a request: OpenAI's user field (end_user, the
-- caller's own name for whoever it serves) and its metadata or
-- x-llmgw-tags (tags, a JSON object of strings, keys sorted). For
-- attributing spend inside a project; never the text.
ALTER TABLE usage ADD COLUMN end_user TEXT NOT NULL DEFAULT '';
ALTER TABLE usage ADD COLUMN tags TEXT NOT NULL DEFAULT '';
@@ -0,0 +1,6 @@
-- What a client said about a request: OpenAI's user field (end_user, the
-- caller's own name for whoever it serves) and its metadata or
-- x-llmgw-tags (tags, a JSON object of strings, keys sorted). For
-- attributing spend inside a project; never the text.
ALTER TABLE usage ADD COLUMN end_user TEXT NOT NULL DEFAULT '';
ALTER TABLE usage ADD COLUMN tags TEXT NOT NULL DEFAULT '';
+8 -5
View File
@@ -430,8 +430,8 @@ func (s *DB) WriteUsage(ctx context.Context, records []Usage) error {
`INSERT INTO usage (ts, key_id, project_id, model, served_model,
prompt_tokens, completion_tokens, cost_micros, status, latency_ms, user_id,
request_id, replica, queue_ms, ttft_ms,
guard_action, guard_policy, guard_category, guard_ms)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
guard_action, guard_policy, guard_category, guard_ms, end_user, tags)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
return err
}
@@ -441,7 +441,7 @@ func (s *DB) WriteUsage(ctx context.Context, records []Usage) error {
r.Model, r.ServedModel, r.PromptTokens, r.CompletionTokens,
r.CostMicros, r.Status, r.LatencyMs, r.UserID,
r.RequestID, r.Replica, r.QueueMs, r.TTFTMs,
r.GuardAction, r.GuardPolicy, r.GuardCategory, r.GuardMs); err != nil {
r.GuardAction, r.GuardPolicy, r.GuardCategory, r.GuardMs, r.EndUser, tagsJSON(r.Tags)); err != nil {
return err
}
}
@@ -850,14 +850,16 @@ func (s *DB) SnapshotTo(ctx context.Context, path string) error {
func (s *DB) UsageByRequest(ctx context.Context, requestID string) (Usage, bool, error) {
var u Usage
var ts int64
var tags string
err := s.db.QueryRowContext(ctx,
`SELECT ts, key_id, project_id, COALESCE(user_id, ''), model, served_model, prompt_tokens,
completion_tokens, cost_micros, status, latency_ms, request_id, replica, queue_ms, ttft_ms,
guard_action, guard_policy, guard_category, guard_ms
guard_action, guard_policy, guard_category, guard_ms, end_user, tags
FROM usage WHERE request_id = ? ORDER BY ts DESC LIMIT 1`, requestID).
Scan(&ts, &u.KeyID, &u.ProjectID, &u.UserID, &u.Model, &u.ServedModel, &u.PromptTokens,
&u.CompletionTokens, &u.CostMicros, &u.Status, &u.LatencyMs, &u.RequestID, &u.Replica,
&u.QueueMs, &u.TTFTMs, &u.GuardAction, &u.GuardPolicy, &u.GuardCategory, &u.GuardMs)
&u.QueueMs, &u.TTFTMs, &u.GuardAction, &u.GuardPolicy, &u.GuardCategory, &u.GuardMs,
&u.EndUser, &tags)
if errors.Is(err, sql.ErrNoRows) {
return Usage{}, false, nil
}
@@ -865,5 +867,6 @@ func (s *DB) UsageByRequest(ctx context.Context, requestID string) (Usage, bool,
return Usage{}, false, err
}
u.At = time.UnixMilli(ts)
u.Tags = parseTags(tags)
return u, true, nil
}
+28
View File
@@ -16,6 +16,7 @@ package store
import (
"context"
"encoding/json"
"errors"
"strings"
"time"
@@ -262,6 +263,33 @@ type Usage struct {
GuardPolicy string
GuardCategory string
GuardMs int64
// EndUser is OpenAI's user field as the client sent it: its own name
// for whoever it serves, not an account here. Tags are its metadata
// and x-llmgw-tags. Both for attributing spend inside a project.
EndUser string
Tags map[string]string
}
// tagsJSON is how tags are kept: a JSON object, keys sorted, or empty.
func tagsJSON(t map[string]string) string {
if len(t) == 0 {
return ""
}
b, _ := json.Marshal(t) // encoding/json sorts map keys
return string(b)
}
// parseTags reads tagsJSON's form back.
func parseTags(s string) map[string]string {
if s == "" {
return nil
}
var t map[string]string
if json.Unmarshal([]byte(s), &t) != nil {
return nil
}
return t
}
// Store is the persistent half of tenancy and accounting.