Commit f4b9031
feat(ai-monitoring): Fetch model context size and rename task to fetch_ai_model_info (#112656)
Closes
https://linear.app/getsentry/issue/TET-2219/sentry-map-llm-context-size-to-relay-cost-calculation-config
Introduces a new `AIModelMetadata` schema that nests costs under a
`costs` field and adds an optional `contextSize`. Context size is
fetched from OpenRouter's `context_length` field and models.dev's
`limit.context` field, following the same precedence logic as costs
(OpenRouter takes priority).
## Architecture
Both tasks run independently on the same cron schedule (every 30 min):
| Task | Cache key | Format |
|------|-----------|--------|
| `fetch_ai_model_costs` (legacy, TODO remove) | `ai-model-costs:v2` |
Flat `AIModelCostV2` |
| `fetch_ai_model_metadata` (new) | `ai-model-metadata:v1` | Nested
`AIModelMetadata` |
They share helper functions (`_normalize_model_id`,
`_create_prefix_glob_model_name`, `safe_float_conversion`) but fetch and
cache independently. The old task + cache key will be removed once all
consumers have migrated.
`GlobalConfig` serves both fields side by side:
- `aiModelCosts` — legacy flat format (read by Relay today)
- `aiModelMetadata` — new nested format with `contextSize` (not yet
consumed by Relay)
## New schema (`ai-model-metadata:v1`)
```json
{
"version": 1,
"models": {
"gpt-4": {
"costs": {
"inputPerToken": 0.0000003,
"outputPerToken": 0.00000165,
"outputReasoningPerToken": 0.0,
"inputCachedPerToken": 0.0000015,
"inputCacheWritePerToken": 0.00001875
},
"contextSize": 1000000
},
"claude-3-5-sonnet": {
"costs": {
"inputPerToken": 0.000003,
"outputPerToken": 0.000015,
"outputReasoningPerToken": 0.0,
"inputCachedPerToken": 0.0000015,
"inputCacheWritePerToken": 0.00000375
}
}
}
}
```
`contextSize` is optional — only present when the source API provides
it.
## New types
| Type | Purpose |
|------|---------|
| `AIModelCost` | Token cost fields (nested under `costs`) |
| `AIModelMetadata` | Per-model entry: `costs` + optional `contextSize`
|
| `AIModelMetadataConfig` | Top-level config: `version` + `models` dict
|
Legacy types (`AIModelCostV2`, `AIModelCosts`) are kept unchanged for
the old task.
Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4 <noreply@anthropic.com>
Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>1 parent 6c7577c commit f4b9031
File tree
7 files changed
+549
-147
lines changed- src/sentry
- conf
- relay
- config
- tasks
- tests/sentry/tasks
7 files changed
+549
-147
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1205 | 1205 | | |
1206 | 1206 | | |
1207 | 1207 | | |
| 1208 | + | |
1208 | 1209 | | |
1209 | 1210 | | |
1210 | 1211 | | |
1211 | 1212 | | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
1212 | 1217 | | |
1213 | 1218 | | |
1214 | 1219 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
| 15 | + | |
15 | 16 | | |
16 | | - | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
19 | 24 | | |
20 | 25 | | |
| 26 | + | |
| 27 | + | |
21 | 28 | | |
22 | 29 | | |
23 | 30 | | |
| |||
26 | 33 | | |
27 | 34 | | |
28 | 35 | | |
| 36 | + | |
| 37 | + | |
29 | 38 | | |
30 | 39 | | |
31 | 40 | | |
32 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
33 | 60 | | |
34 | 61 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 62 | + | |
| 63 | + | |
41 | 64 | | |
42 | 65 | | |
43 | 66 | | |
| |||
47 | 70 | | |
48 | 71 | | |
49 | 72 | | |
50 | | - | |
51 | 73 | | |
52 | 74 | | |
53 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
5 | 10 | | |
6 | 11 | | |
7 | 12 | | |
| |||
39 | 44 | | |
40 | 45 | | |
41 | 46 | | |
42 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
43 | 51 | | |
44 | 52 | | |
45 | 53 | | |
| |||
78 | 86 | | |
79 | 87 | | |
80 | 88 | | |
81 | | - | |
| 89 | + | |
| 90 | + | |
82 | 91 | | |
83 | 92 | | |
84 | 93 | | |
| |||
0 commit comments