-
Notifications
You must be signed in to change notification settings - Fork 1
Report Anthropic AI Model Usage #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
179b294
Report Anthropic AI Usage
reiniercriel 0a1cc25
Some adjustments
reiniercriel b52e821
Cleanup doc
reiniercriel 1f99205
Some cleanup
reiniercriel 365ef7e
Some more cleanup
reiniercriel 6c5c0f9
Cleaup test
reiniercriel 32e324b
Remove unneeded comments
reiniercriel c232756
Fix comments
reiniercriel 340e3d1
Merge branch 'main' into feat/anthropic-report-model-usage
reiniercriel 45f0a5e
Merge remote-tracking branch 'origin/main' into feat/anthropic-reportβ¦
reiniercriel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package ingress | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "log" | ||
| "net/http" | ||
| ) | ||
|
|
||
| func (s *Server) handleAiUsage(w http.ResponseWriter, r *http.Request) { | ||
| log.Printf("ai-usage: POST /events/ai-usage from %s", r.RemoteAddr) | ||
|
|
||
| var event AiUsageEvent | ||
| if err := json.NewDecoder(r.Body).Decode(&event); err != nil { | ||
| log.Printf("ai-usage: invalid JSON body: %v", err) | ||
| http.Error(w, "invalid request body", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| if event.Provider == "" || event.Model == "" { | ||
| log.Printf("ai-usage: rejecting event with missing fields: provider=%q model=%q", event.Provider, event.Model) | ||
| http.Error(w, "provider and model are required", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| stored, isNew := s.aiUsageStore.Add(event) | ||
| if isNew { | ||
| log.Printf("ai-usage: first observation: provider=%s model=%s", stored.Provider, stored.Model) | ||
| } else { | ||
| log.Printf("ai-usage: repeat observation: provider=%s model=%s", stored.Provider, stored.Model) | ||
| } | ||
|
|
||
| w.WriteHeader(http.StatusOK) | ||
| } | ||
|
|
||
| func (s *Server) handleAiUsageEvents(w http.ResponseWriter, r *http.Request) { | ||
| if !s.validateUIToken(w, r) { | ||
| return | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusOK) | ||
| if err := json.NewEncoder(w).Encode(s.aiUsageStore.List()); err != nil { | ||
| log.Printf("failed to encode ai-usage events: %v", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package ingress | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
| ) | ||
|
|
||
| type aiUsageEventStore struct { | ||
| mu sync.RWMutex | ||
| events []AiUsageEvent | ||
| } | ||
|
|
||
| func aiUsageEventID(provider, model string) string { | ||
| return fmt.Sprintf("ai-usage-%s-%s", provider, model) | ||
| } | ||
|
|
||
| // Add records an observation for the given (provider, model) pair. Repeats | ||
| // just refresh the stored timestamp. Returns the stored row and `isNew=true` | ||
| // when this was the first observation of that (provider, model). | ||
| func (s *aiUsageEventStore) Add(ev AiUsageEvent) (AiUsageEvent, bool) { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
|
|
||
| id := aiUsageEventID(ev.Provider, ev.Model) | ||
|
|
||
| for i := range s.events { | ||
| if s.events[i].ID == id { | ||
| s.events[i].TsMs = ev.TsMs | ||
| return s.events[i], false | ||
| } | ||
| } | ||
|
|
||
| stored := AiUsageEvent{ | ||
| ID: id, | ||
| TsMs: ev.TsMs, | ||
| Provider: ev.Provider, | ||
| Model: ev.Model, | ||
| } | ||
| s.events = append(s.events, stored) | ||
| return stored, true | ||
| } | ||
|
|
||
| func (s *aiUsageEventStore) List() []AiUsageEvent { | ||
| s.mu.RLock() | ||
| defer s.mu.RUnlock() | ||
| out := make([]AiUsageEvent, len(s.events)) | ||
| copy(out, s.events) | ||
| return out | ||
| } | ||
|
|
||
| func (s *Server) AiUsageEvents() []AiUsageEvent { | ||
| return s.aiUsageStore.List() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package ingress | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestAiUsageEventStoreAddAssignsStableIDAndReportsNew(t *testing.T) { | ||
| store := &aiUsageEventStore{} | ||
|
|
||
| stored, isNew := store.Add(AiUsageEvent{ | ||
| TsMs: 100, | ||
| Provider: "anthropic", | ||
| Model: "claude-3-5-sonnet-20241022", | ||
| }) | ||
|
|
||
| want := "ai-usage-anthropic-claude-3-5-sonnet-20241022" | ||
| if stored.ID != want { | ||
| t.Fatalf("expected stable id %q, got %q", want, stored.ID) | ||
| } | ||
| if !isNew { | ||
| t.Fatalf("expected first observation to report isNew=true") | ||
| } | ||
| if stored.TsMs != 100 { | ||
| t.Fatalf("expected stored ts_ms=100, got %d", stored.TsMs) | ||
| } | ||
| } | ||
|
|
||
| func TestAiUsageEventStoreAddCollapsesSameModelAndRefreshesTimestamp(t *testing.T) { | ||
| store := &aiUsageEventStore{} | ||
|
|
||
| first, firstIsNew := store.Add(AiUsageEvent{TsMs: 100, Provider: "anthropic", Model: "claude-opus-4-7"}) | ||
| second, secondIsNew := store.Add(AiUsageEvent{TsMs: 250, Provider: "anthropic", Model: "claude-opus-4-7"}) | ||
| third, thirdIsNew := store.Add(AiUsageEvent{TsMs: 400, Provider: "anthropic", Model: "claude-opus-4-7"}) | ||
|
|
||
| if !firstIsNew { | ||
| t.Fatalf("expected first call to report isNew=true") | ||
| } | ||
| if secondIsNew || thirdIsNew { | ||
| t.Fatalf("expected repeats to report isNew=false, got second=%v third=%v", secondIsNew, thirdIsNew) | ||
| } | ||
| if first.ID != second.ID || second.ID != third.ID { | ||
| t.Fatalf("expected stable aggregate id, got %q / %q / %q", first.ID, second.ID, third.ID) | ||
| } | ||
| if len(store.List()) != 1 { | ||
| t.Fatalf("expected one aggregate event, got %d", len(store.List())) | ||
| } | ||
| if third.TsMs != 400 { | ||
| t.Fatalf("expected ts_ms to refresh to 400, got %d", third.TsMs) | ||
| } | ||
| } | ||
|
|
||
| func TestAiUsageEventStoreAddSeparatesEntriesPerModel(t *testing.T) { | ||
| store := &aiUsageEventStore{} | ||
|
|
||
| store.Add(AiUsageEvent{TsMs: 100, Provider: "anthropic", Model: "claude-opus-4-7"}) | ||
| store.Add(AiUsageEvent{TsMs: 200, Provider: "anthropic", Model: "claude-haiku-4-5"}) | ||
|
|
||
| if len(store.List()) != 2 { | ||
| t.Fatalf("expected one aggregate event per model, got %d", len(store.List())) | ||
| } | ||
| } | ||
|
|
||
| func TestAiUsageEventStoreAddSeparatesEntriesPerProvider(t *testing.T) { | ||
| store := &aiUsageEventStore{} | ||
|
|
||
| store.Add(AiUsageEvent{TsMs: 100, Provider: "anthropic", Model: "shared-name"}) | ||
| store.Add(AiUsageEvent{TsMs: 200, Provider: "openai", Model: "shared-name"}) | ||
|
|
||
| if len(store.List()) != 2 { | ||
| t.Fatalf("expected one aggregate event per provider, got %d", len(store.List())) | ||
| } | ||
| } | ||
|
|
||
| func TestServerAiUsageEventsReturnsCopyAndDoesNotClear(t *testing.T) { | ||
| s := &Server{aiUsageStore: &aiUsageEventStore{}} | ||
|
|
||
| s.aiUsageStore.Add(AiUsageEvent{TsMs: 100, Provider: "anthropic", Model: "claude-opus-4-7"}) | ||
| s.aiUsageStore.Add(AiUsageEvent{TsMs: 200, Provider: "anthropic", Model: "claude-opus-4-7"}) | ||
|
|
||
| snap1 := s.AiUsageEvents() | ||
| if len(snap1) != 1 { | ||
| t.Fatalf("expected one row in snapshot, got %d", len(snap1)) | ||
| } | ||
| if snap1[0].TsMs != 200 { | ||
| t.Fatalf("expected ts_ms=200 (latest), got %d", snap1[0].TsMs) | ||
| } | ||
|
|
||
| // Mutating the returned slice must not affect the store. | ||
| snap1[0].TsMs = 999 | ||
| snap2 := s.AiUsageEvents() | ||
| if snap2[0].TsMs != 200 { | ||
| t.Fatalf("snapshot must be a copy; store was mutated to ts_ms=%d", snap2[0].TsMs) | ||
| } | ||
|
|
||
| // A second call after a flush-equivalent call must still see the data β | ||
| // the store is intentionally not cleared. | ||
| if len(snap2) != 1 { | ||
| t.Fatalf("expected store to retain data, got %d rows", len(snap2)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.