ctxtop 0.0.0-stage → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.antigravity-plugin/plugin.json +36 -0
- package/.claude-plugin/plugin.json +18 -0
- package/CHECKSUMS.sha256 +149 -0
- package/LICENSE +27 -0
- package/README.md +330 -3
- package/SECURITY.md +22 -0
- package/agents/ah-flash-scout.md +25 -0
- package/assets/advanced_view_loaded_420px.png +0 -0
- package/assets/hero-demo.svg +188 -0
- package/assets/logo.svg +19 -0
- package/assets/simple_view_lean_420px.png +0 -0
- package/assets/simple_view_loaded_420px.png +0 -0
- package/assets/social-preview.svg +72 -0
- package/assets/zone_c_step3_shield_full.png +0 -0
- package/bin/ah-statusline.mjs +145 -0
- package/bin/cli.mjs +3 -0
- package/bin/ctxtop.mjs +1474 -0
- package/commands/cost-radar.md +11 -0
- package/commands/evolve.md +10 -0
- package/commands/handoff.md +10 -0
- package/commands/horizon.md +10 -0
- package/commands/inspect.md +10 -0
- package/commands/precheck.md +10 -0
- package/config/default_flightcheck.json +114 -0
- package/config/modelCatalog.json +620 -0
- package/config/platformQuirks.json +19 -0
- package/hooks/handlers/compact-lifecycle.mjs +262 -0
- package/hooks/handlers/hookIo.mjs +255 -0
- package/hooks/handlers/hookIpcClient.mjs +83 -0
- package/hooks/handlers/post-tool-use.mjs +265 -0
- package/hooks/handlers/pre-model-switch.mjs +249 -0
- package/hooks/handlers/pre-tool-use.mjs +365 -0
- package/hooks/handlers/stop-gate.mjs +240 -0
- package/hooks/handlers/user-prompt-submit.mjs +143 -0
- package/hooks/hooks.json +81 -0
- package/hooks/post_invocation.mjs +13 -0
- package/hooks/post_tool_use.mjs +17 -0
- package/hooks/pre_invocation.mjs +14 -0
- package/hooks/pre_tool_use.mjs +97 -0
- package/hooks/register.js +687 -0
- package/hooks/session_start.mjs +14 -0
- package/hooks/stop.mjs +13 -0
- package/hooks.json +56 -0
- package/package.json +103 -4
- package/packages/antigravity/package.json +14 -0
- package/packages/antigravity/src/index.mjs +1 -0
- package/packages/claude-code/package.json +14 -0
- package/packages/claude-code/src/index.mjs +2 -0
- package/packages/core/package.json +14 -0
- package/packages/core/src/index.mjs +1 -0
- package/packages/engine-node/package.json +14 -0
- package/packages/engine-node/src/index.mjs +1 -0
- package/packages/mcp/package.json +17 -0
- package/packages/mcp/src/server.mjs +396 -0
- package/packages/pro/LICENSE-COMMERCIAL.md +19 -0
- package/packages/pro/README-SIGNING.md +30 -0
- package/packages/pro/package.json +16 -0
- package/packages/pro/src/index.mjs +3 -0
- package/packages/pro/src/team_finops_pack.mjs +241 -0
- package/packages/pro/workers/polar_webhook_worker.mjs +215 -0
- package/plugin.json +36 -0
- package/rules/AGENTS.md +10 -0
- package/sidecars/horizon-daemon/ipcServer.mjs +24 -0
- package/sidecars/horizon-daemon/server.mjs +2002 -0
- package/sidecars/horizon-daemon/sidecar.json +19 -0
- package/skills/cost-radar/SKILL.md +21 -0
- package/skills/evolve/SKILL.md +21 -0
- package/skills/handoff/SKILL.md +42 -0
- package/skills/horizon/SKILL.md +17 -0
- package/skills/inspect/SKILL.md +18 -0
- package/skills/precheck/SKILL.md +44 -0
- package/src/adapters/antigravity/index.mjs +79 -0
- package/src/adapters/claude-code/index.mjs +155 -0
- package/src/cli/cost_radar.mjs +70 -0
- package/src/cli/doctor.mjs +351 -0
- package/src/cli/horizon_hud.mjs +686 -0
- package/src/cli/init.mjs +425 -0
- package/src/cli/postmortem.mjs +515 -0
- package/src/coach/cacheGravityRouter.mjs +12 -0
- package/src/config/modelCatalog.json +620 -0
- package/src/config/platformQuirks.json +19 -0
- package/src/core/action_queue.mjs +264 -0
- package/src/core/ag_bridge.mjs +346 -0
- package/src/core/atomic_io.mjs +124 -0
- package/src/core/baseline.mjs +607 -0
- package/src/core/blindspot_sensors.mjs +399 -0
- package/src/core/classifier.mjs +1757 -0
- package/src/core/coach/circuit_breakers.mjs +650 -0
- package/src/core/coach/contradiction_auditor.mjs +239 -0
- package/src/core/coach/effort_advisor.mjs +143 -0
- package/src/core/coach/handoff_hydrator.mjs +483 -0
- package/src/core/coach/progressive_checkpointer.mjs +551 -0
- package/src/core/coach/prompt_decomposer_coach.mjs +141 -0
- package/src/core/coach/reflector_queue.mjs +505 -0
- package/src/core/coach/saved_quota_ledger.mjs +615 -0
- package/src/core/coach/subagent_ipi_sanitizer.mjs +200 -0
- package/src/core/coach/subagent_phantom_auditor.mjs +261 -0
- package/src/core/coach/task_quota_router.mjs +955 -0
- package/src/core/coach/turn_zero_gate.mjs +574 -0
- package/src/core/compaction_coach.mjs +1126 -0
- package/src/core/config.mjs +214 -0
- package/src/core/constants.mjs +1800 -0
- package/src/core/detectors_det01_to_16.mjs +1474 -0
- package/src/core/episodic_memory.mjs +135 -0
- package/src/core/fts5_output_compressor.mjs +359 -0
- package/src/core/git_ledger.mjs +189 -0
- package/src/core/handoff_audit.mjs +558 -0
- package/src/core/history.mjs +221 -0
- package/src/core/hook_diagnostics.mjs +158 -0
- package/src/core/horizon_classifier.mjs +326 -0
- package/src/core/horizon_config.mjs +802 -0
- package/src/core/host_watchdog.mjs +200 -0
- package/src/core/layer5_hygiene.mjs +966 -0
- package/src/core/log_resolver.mjs +231 -0
- package/src/core/mcp_auditor.mjs +138 -0
- package/src/core/micro_skills_store.mjs +91 -0
- package/src/core/offenders.mjs +161 -0
- package/src/core/parser.mjs +1996 -0
- package/src/core/path_canonicalizer.mjs +73 -0
- package/src/core/paths.mjs +310 -0
- package/src/core/prefix_cache_shield.mjs +386 -0
- package/src/core/pro_license.mjs +385 -0
- package/src/core/prompt_rubric.mjs +518 -0
- package/src/core/pruner.mjs +433 -0
- package/src/core/quota.mjs +1860 -0
- package/src/core/rsi_instinct_engine.mjs +1197 -0
- package/src/core/sanitizer.mjs +121 -0
- package/src/core/session_forensics.mjs +303 -0
- package/src/core/session_timeline.mjs +358 -0
- package/src/core/settings.mjs +99 -0
- package/src/core/shared/detectors.mjs +1342 -0
- package/src/core/shared/fnv1a64.mjs +85 -0
- package/src/core/shared/handoff_schema.mjs +123 -0
- package/src/core/shared/index.mjs +5 -0
- package/src/core/shared/ipi_sanitizer.mjs +165 -0
- package/src/core/shared/micro_skills.mjs +1 -0
- package/src/core/shared/plugin_validator.mjs +202 -0
- package/src/core/shared/prompt_hygiene_governor.mjs +1 -0
- package/src/core/sidecar_store.mjs +302 -0
- package/src/core/sprint_metrics.mjs +366 -0
- package/src/core/sqlite_client.mjs +283 -0
- package/src/core/storage_doctor.mjs +358 -0
- package/src/core/subagents.mjs +609 -0
- package/src/core/swarm_topology.mjs +360 -0
- package/src/core/task_quota_router.mjs +1656 -0
- package/src/core/telemetry/cache_ttl_watchdog.mjs +284 -0
- package/src/core/telemetry/scope_lock_registry.mjs +578 -0
- package/src/core/telemetry/shadow_file_detector.mjs +291 -0
- package/src/core/telemetry/trajectory_watcher.mjs +259 -0
- package/src/core/timeline_svg.mjs +236 -0
- package/src/core/tool_surgeon.mjs +305 -0
- package/src/core/trajectory_stitcher.mjs +645 -0
- package/src/core/u_shaped_optimizer.mjs +789 -0
- package/src/core/wal_hygiene.mjs +233 -0
- package/src/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
- package/src/core-isomorphic/catalog/index.mjs +1 -0
- package/src/core-isomorphic/coach/cacheGravityRouter.mjs +720 -0
- package/src/core-isomorphic/coach/microSkills.mjs +1 -0
- package/src/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
- package/src/core-isomorphic/coach/repoCalibration.mjs +254 -0
- package/src/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
- package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +457 -0
- package/src/core-isomorphic/config/anthropicModelCatalog.json +203 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +768 -0
- package/src/core-isomorphic/config/horizonConfig.mjs +1009 -0
- package/src/core-isomorphic/control/callTree.mjs +338 -0
- package/src/core-isomorphic/control/coachLatches.mjs +126 -0
- package/src/core-isomorphic/control/controlStateMachine.mjs +205 -0
- package/src/core-isomorphic/control/index.mjs +49 -0
- package/src/core-isomorphic/control/swarmWorktree.mjs +109 -0
- package/src/core-isomorphic/control/toolSuspension.mjs +197 -0
- package/src/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
- package/src/core-isomorphic/diagnostics/index.mjs +9 -0
- package/src/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
- package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +622 -0
- package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +351 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +170 -0
- package/src/core-isomorphic/economics/driftLatency.mjs +319 -0
- package/src/core-isomorphic/economics/dualTtl.mjs +198 -0
- package/src/core-isomorphic/economics/index.mjs +47 -0
- package/src/core-isomorphic/economics/retentionRadar.mjs +348 -0
- package/src/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1035 -0
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +480 -0
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +603 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +301 -0
- package/src/core-isomorphic/index.js +26 -0
- package/src/core-isomorphic/telemetry/otelExporter.mjs +187 -0
- package/src/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
- package/src/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
- package/src/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
- package/src/core-isomorphic/telemetry/telemetryBridge.mjs +1132 -0
- package/src/core-isomorphic/ui/brailleSparkline.mjs +142 -0
- package/src/core-isomorphic/ui/costRadarSvg.mjs +276 -0
- package/src/core-isomorphic/ui/horizonGrid.mjs +475 -0
- package/src/core-isomorphic/ui/horizonPanelHtml.mjs +1181 -0
- package/src/core-isomorphic/ui/modViews.mjs +1478 -0
- package/src/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
- package/src/core-isomorphic/ustf/index.mjs +7 -0
- package/src/core-isomorphic/ustf/normalizer.mjs +512 -0
- package/src/core-isomorphic/ustf/schema.mjs +186 -0
- package/src/core-isomorphic/utils/runtime.mjs +962 -0
- package/src/demo/synthetic_states.mjs +661 -0
- package/src/diagnostics/bugReportGenerator.mjs +253 -0
- package/src/diagnostics/doctorEngine.mjs +203 -0
- package/src/engine-node/db/index.mjs +58 -0
- package/src/engine-node/db/jsonl_fallback.mjs +184 -0
- package/src/engine-node/db/probe.mjs +110 -0
- package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
- package/src/engine-node/index.mjs +9 -0
- package/src/engine-node/ipc/client.mjs +447 -0
- package/src/engine-node/ipc/index.mjs +22 -0
- package/src/engine-node/ipc/protocol.mjs +177 -0
- package/src/engine-node/ipc/server.mjs +445 -0
- package/src/engine-node/offloader/index.mjs +6 -0
- package/src/engine-node/offloader/tool_offloader.mjs +241 -0
- package/src/engine-node/sensors/blindspots.mjs +369 -0
- package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
- package/src/engine-node/sensors/index.mjs +7 -0
- package/src/hooks/portfolio_runtime.mjs +858 -0
- package/src/hooks/post_invocation.mjs +419 -0
- package/src/hooks/pre_invocation.mjs +1015 -0
- package/src/hooks/session_start.mjs +161 -0
- package/src/shims/agy_cli_compressor.mjs +705 -0
- package/src/shims/agy_git_guard.mjs +749 -0
- package/src/shims/ah_log_query.mjs +39 -0
- package/src/ui/costRadarSvg.mjs +6 -0
- package/src/ui/horizonGrid.mjs +1 -0
- package/src/ui/horizonPanelHtml.mjs +1 -0
- package/src/ui/horizon_panel.css +167 -0
- package/src/ui/horizon_panel.js +212 -0
- package/src/ui/modViews.mjs +1 -0
- package/src/utils/nodeIo.mjs +59 -0
- package/src/utils/runtime.mjs +1 -0
- package/src/webview/advanced_view.html +1808 -0
- package/src/webview/horizon_panel.html +11389 -0
- package/src/webview/registry.mjs +80 -0
- package/src/webview/simple_view.html +1370 -0
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/config/catalogLoader.mjs
|
|
3
|
+
* Unified Anthropic Model Catalog loader, rate card resolver, Haiku 5.5 100k 5x cliff calculator,
|
|
4
|
+
* and customCeilings.json override merger.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
path,
|
|
9
|
+
getCwd,
|
|
10
|
+
getHomeDir,
|
|
11
|
+
getNodeIo,
|
|
12
|
+
fileUrlToPathPure,
|
|
13
|
+
readJsonSafe
|
|
14
|
+
} from '../utils/runtime.mjs';
|
|
15
|
+
|
|
16
|
+
export const BUILTIN_CATALOG = {
|
|
17
|
+
schemaVersion: '2026.11.1',
|
|
18
|
+
defaultModel: 'claude-sonnet-5-5',
|
|
19
|
+
cacheWriteMultipliers: {
|
|
20
|
+
'5m': 1.25,
|
|
21
|
+
'1h': 2.0
|
|
22
|
+
},
|
|
23
|
+
effortExpectedOutputTokens: {
|
|
24
|
+
low: 800,
|
|
25
|
+
medium: 1500,
|
|
26
|
+
high: 3500,
|
|
27
|
+
max: 6000
|
|
28
|
+
},
|
|
29
|
+
models: {
|
|
30
|
+
'claude-fable-5-2': {
|
|
31
|
+
id: 'claude-fable-5-2',
|
|
32
|
+
label: 'Claude Fable 5.2',
|
|
33
|
+
shortLabel: 'Fable 5.2',
|
|
34
|
+
family: 'fable',
|
|
35
|
+
generation: '5.2',
|
|
36
|
+
contextCeiling: 1000000,
|
|
37
|
+
operationalSoftCeiling: 180000,
|
|
38
|
+
ratesPer1M: { input: 8.0, output: 40.0, write5m: 10.0, write1h: 16.0, read: 0.2 },
|
|
39
|
+
multipliers: { write5m: 1.25, write1h: 2.0, read: 0.025 },
|
|
40
|
+
pricingCliff: null
|
|
41
|
+
},
|
|
42
|
+
'claude-fable-5-1': {
|
|
43
|
+
id: 'claude-fable-5-1',
|
|
44
|
+
label: 'Claude Fable 5.1',
|
|
45
|
+
shortLabel: 'Fable 5.1',
|
|
46
|
+
family: 'fable',
|
|
47
|
+
generation: '5.1',
|
|
48
|
+
contextCeiling: 1000000,
|
|
49
|
+
operationalSoftCeiling: 180000,
|
|
50
|
+
ratesPer1M: { input: 10.0, output: 50.0, write5m: 12.5, write1h: 20.0, read: 0.25 },
|
|
51
|
+
multipliers: { write5m: 1.25, write1h: 2.0, read: 0.025 },
|
|
52
|
+
pricingCliff: null
|
|
53
|
+
},
|
|
54
|
+
'claude-opus-5-5': {
|
|
55
|
+
id: 'claude-opus-5-5',
|
|
56
|
+
label: 'Claude Opus 5.5',
|
|
57
|
+
shortLabel: 'Opus 5.5',
|
|
58
|
+
family: 'opus',
|
|
59
|
+
generation: '5.5',
|
|
60
|
+
contextCeiling: 1000000,
|
|
61
|
+
operationalSoftCeiling: 180000,
|
|
62
|
+
ratesPer1M: { input: 4.0, output: 20.0, write5m: 5.0, write1h: 8.0, read: 0.2 },
|
|
63
|
+
multipliers: { write5m: 1.25, write1h: 2.0, read: 0.05 },
|
|
64
|
+
pricingCliff: null
|
|
65
|
+
},
|
|
66
|
+
'claude-sonnet-5-5': {
|
|
67
|
+
id: 'claude-sonnet-5-5',
|
|
68
|
+
label: 'Claude Sonnet 5.5',
|
|
69
|
+
shortLabel: 'Sonnet 5.5',
|
|
70
|
+
family: 'sonnet',
|
|
71
|
+
generation: '5.5',
|
|
72
|
+
contextCeiling: 1000000,
|
|
73
|
+
operationalSoftCeiling: 180000,
|
|
74
|
+
ratesPer1M: { input: 2.0, output: 10.0, write5m: 2.5, write1h: 4.0, read: 0.1 },
|
|
75
|
+
multipliers: { write5m: 1.25, write1h: 2.0, read: 0.05 },
|
|
76
|
+
pricingCliff: null
|
|
77
|
+
},
|
|
78
|
+
'claude-haiku-5-5': {
|
|
79
|
+
id: 'claude-haiku-5-5',
|
|
80
|
+
label: 'Claude Haiku 5.5',
|
|
81
|
+
shortLabel: 'Haiku 5.5',
|
|
82
|
+
family: 'haiku',
|
|
83
|
+
generation: '5.5',
|
|
84
|
+
contextCeiling: 1000000,
|
|
85
|
+
operationalSoftCeiling: 90000,
|
|
86
|
+
ratesPer1M: { input: 0.1, output: 0.5, write5m: 0.125, write1h: 0.2, read: 0.01 },
|
|
87
|
+
multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
|
|
88
|
+
pricingCliff: {
|
|
89
|
+
thresholdTokens: 100000,
|
|
90
|
+
preCliffWarningTokens: 90000,
|
|
91
|
+
multiplier: 5.0,
|
|
92
|
+
ratesPer1MAboveCliff: {
|
|
93
|
+
input: 0.5,
|
|
94
|
+
output: 2.5,
|
|
95
|
+
write5m: 0.625,
|
|
96
|
+
write1h: 1.0,
|
|
97
|
+
read: 0.05
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
'claude-opus-4-6': {
|
|
102
|
+
id: 'claude-opus-4-6',
|
|
103
|
+
label: 'Claude Opus 4.6',
|
|
104
|
+
shortLabel: 'Opus 4.6',
|
|
105
|
+
family: 'opus',
|
|
106
|
+
generation: '4.6',
|
|
107
|
+
contextCeiling: 200000,
|
|
108
|
+
operationalSoftCeiling: 160000,
|
|
109
|
+
ratesPer1M: { input: 5.0, output: 25.0, write5m: 6.25, write1h: 10.0, read: 0.5 },
|
|
110
|
+
multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
|
|
111
|
+
pricingCliff: null
|
|
112
|
+
},
|
|
113
|
+
'claude-sonnet-4-6': {
|
|
114
|
+
id: 'claude-sonnet-4-6',
|
|
115
|
+
label: 'Claude Sonnet 4.6',
|
|
116
|
+
shortLabel: 'Sonnet 4.6',
|
|
117
|
+
family: 'sonnet',
|
|
118
|
+
generation: '4.6',
|
|
119
|
+
contextCeiling: 200000,
|
|
120
|
+
operationalSoftCeiling: 160000,
|
|
121
|
+
ratesPer1M: { input: 3.0, output: 15.0, write5m: 3.75, write1h: 6.0, read: 0.3 },
|
|
122
|
+
multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
|
|
123
|
+
pricingCliff: null
|
|
124
|
+
},
|
|
125
|
+
'claude-haiku-4-5': {
|
|
126
|
+
id: 'claude-haiku-4-5',
|
|
127
|
+
label: 'Claude Haiku 4.5',
|
|
128
|
+
shortLabel: 'Haiku 4.5',
|
|
129
|
+
family: 'haiku',
|
|
130
|
+
generation: '4.5',
|
|
131
|
+
contextCeiling: 200000,
|
|
132
|
+
operationalSoftCeiling: 160000,
|
|
133
|
+
ratesPer1M: { input: 1.0, output: 5.0, write5m: 1.25, write1h: 2.0, read: 0.1 },
|
|
134
|
+
multipliers: { write5m: 1.25, write1h: 2.0, read: 0.1 },
|
|
135
|
+
pricingCliff: null
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Resolves the directory containing this module when Tier-2 Node I/O is active.
|
|
142
|
+
* @param {object} [options={}]
|
|
143
|
+
* @returns {string | null}
|
|
144
|
+
*/
|
|
145
|
+
function getModuleDir(options = {}) {
|
|
146
|
+
if (!getNodeIo(options)) return null;
|
|
147
|
+
try {
|
|
148
|
+
const filePath = fileUrlToPathPure(import.meta.url, options);
|
|
149
|
+
return filePath ? path.dirname(filePath) : null;
|
|
150
|
+
} catch (_err) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Detects whether a raw model identifier or options flag requests a 1,000,000-token
|
|
157
|
+
* extended context window (`[1m]`, `-1m`, `_1m`, or `options.extendedContext1M === true`).
|
|
158
|
+
*
|
|
159
|
+
* @param {string} [rawModelId='']
|
|
160
|
+
* @param {object} [options={}]
|
|
161
|
+
* @returns {boolean}
|
|
162
|
+
*/
|
|
163
|
+
export function hasExtended1MHint(rawModelId = '', options = {}) {
|
|
164
|
+
if (options?.extendedContext1M === true) return true;
|
|
165
|
+
const raw = String(rawModelId || '').toLowerCase().trim();
|
|
166
|
+
return /\[1m\]|(?:^|[-_:\s])1m(?:$|[-_:\s\]])/.test(raw);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Normalizes any raw Anthropic model string or shorthand alias to its canonical catalog ID.
|
|
171
|
+
* @param {string} [rawModelId='']
|
|
172
|
+
* @returns {string}
|
|
173
|
+
*/
|
|
174
|
+
export function normalizeModelId(rawModelId = '') {
|
|
175
|
+
const cleaned = String(rawModelId || '')
|
|
176
|
+
.toLowerCase()
|
|
177
|
+
.trim()
|
|
178
|
+
.replace(/\[1m\]/g, '')
|
|
179
|
+
.replace(/[-_:\s]+1m$/g, '')
|
|
180
|
+
.replace(/\s+/g, '-')
|
|
181
|
+
.replace(/\./g, '-');
|
|
182
|
+
|
|
183
|
+
if (!cleaned) return BUILTIN_CATALOG.defaultModel;
|
|
184
|
+
if (BUILTIN_CATALOG.models[cleaned]) return cleaned;
|
|
185
|
+
|
|
186
|
+
// Fable family
|
|
187
|
+
if (cleaned.includes('fable')) {
|
|
188
|
+
if (cleaned.includes('5-1')) return 'claude-fable-5-1';
|
|
189
|
+
return 'claude-fable-5-2';
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Opus family
|
|
193
|
+
if (cleaned.includes('opus')) {
|
|
194
|
+
if (cleaned.includes('4-6') || cleaned.includes('4-5') || cleaned.includes('4-0') || cleaned.includes('3-')) {
|
|
195
|
+
return 'claude-opus-4-6';
|
|
196
|
+
}
|
|
197
|
+
return 'claude-opus-5-5';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Sonnet family
|
|
201
|
+
if (cleaned.includes('sonnet')) {
|
|
202
|
+
if (cleaned.includes('4-6') || cleaned.includes('4-5') || cleaned.includes('3-7') || cleaned.includes('3-5')) {
|
|
203
|
+
return 'claude-sonnet-4-6';
|
|
204
|
+
}
|
|
205
|
+
return 'claude-sonnet-5-5';
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Haiku family
|
|
209
|
+
if (cleaned.includes('haiku')) {
|
|
210
|
+
if (cleaned.includes('4-5') || cleaned.includes('3-5') || cleaned.includes('3-0')) {
|
|
211
|
+
return 'claude-haiku-4-5';
|
|
212
|
+
}
|
|
213
|
+
return 'claude-haiku-5-5';
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return BUILTIN_CATALOG.defaultModel;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Discovers and reads customCeilings.json from options or standard workspace/user paths.
|
|
221
|
+
* @param {object} [options={}]
|
|
222
|
+
* @returns {object | null}
|
|
223
|
+
*/
|
|
224
|
+
export function loadCustomCeilings(options = {}) {
|
|
225
|
+
if (options.customCeilings && typeof options.customCeilings === 'object') {
|
|
226
|
+
return options.customCeilings;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (!getNodeIo(options)) return null;
|
|
230
|
+
|
|
231
|
+
const candidatePaths = [];
|
|
232
|
+
if (typeof options.customCeilingsPath === 'string' && options.customCeilingsPath.trim()) {
|
|
233
|
+
candidatePaths.push(options.customCeilingsPath.trim());
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const cwd = options.cwd || getCwd(options);
|
|
237
|
+
if (cwd && cwd !== '.') {
|
|
238
|
+
candidatePaths.push(path.join(cwd, '.claude', 'attention-horizon', 'customCeilings.json'));
|
|
239
|
+
candidatePaths.push(path.join(cwd, 'customCeilings.json'));
|
|
240
|
+
candidatePaths.push(path.join(cwd, '.claude', 'customCeilings.json'));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const modDir = getModuleDir(options);
|
|
244
|
+
if (modDir) {
|
|
245
|
+
candidatePaths.push(path.join(modDir, 'customCeilings.json'));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const homeDir = getHomeDir(options);
|
|
249
|
+
if (homeDir) {
|
|
250
|
+
candidatePaths.push(path.join(homeDir, '.claude', 'attention-horizon', 'customCeilings.json'));
|
|
251
|
+
candidatePaths.push(path.join(homeDir, '.claude', 'customCeilings.json'));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
for (const candidate of candidatePaths) {
|
|
255
|
+
const parsed = readJsonSafe(candidate, null, options);
|
|
256
|
+
if (parsed && typeof parsed === 'object') {
|
|
257
|
+
return parsed;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Resolves an explicit user ceiling override (in tokens) for `modelId` from `customCeilings.json`
|
|
266
|
+
* or `options.customCeilings`, returning `null` when no custom ceiling override applies (`ISS-005`).
|
|
267
|
+
*
|
|
268
|
+
* @param {string} [modelId='']
|
|
269
|
+
* @param {object} [options={}]
|
|
270
|
+
* @returns {number | null}
|
|
271
|
+
*/
|
|
272
|
+
export function resolveCustomCeilingForModel(modelId = '', options = {}) {
|
|
273
|
+
const customCeilings = loadCustomCeilings(options);
|
|
274
|
+
if (!customCeilings || typeof customCeilings !== 'object') {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const sourceMap =
|
|
279
|
+
customCeilings.models && typeof customCeilings.models === 'object'
|
|
280
|
+
? customCeilings.models
|
|
281
|
+
: customCeilings.ceilings && typeof customCeilings.ceilings === 'object'
|
|
282
|
+
? customCeilings.ceilings
|
|
283
|
+
: customCeilings;
|
|
284
|
+
|
|
285
|
+
const extractCeilingNumber = (val) => {
|
|
286
|
+
if (typeof val === 'number' && val > 0) return val;
|
|
287
|
+
if (val && typeof val === 'object') {
|
|
288
|
+
if (typeof val.contextCeiling === 'number' && val.contextCeiling > 0) {
|
|
289
|
+
return val.contextCeiling;
|
|
290
|
+
}
|
|
291
|
+
if (typeof val.ceiling === 'number' && val.ceiling > 0) {
|
|
292
|
+
return val.ceiling;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const normId = normalizeModelId(modelId);
|
|
299
|
+
if (modelId && Object.prototype.hasOwnProperty.call(sourceMap, modelId)) {
|
|
300
|
+
const direct = extractCeilingNumber(sourceMap[modelId]);
|
|
301
|
+
if (direct !== null) return direct;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
for (const [rawKey, overrideVal] of Object.entries(sourceMap)) {
|
|
305
|
+
if (rawKey === '*' || rawKey === 'schemaVersion') continue;
|
|
306
|
+
if (rawKey === normId || normalizeModelId(rawKey) === normId) {
|
|
307
|
+
const matched = extractCeilingNumber(overrideVal);
|
|
308
|
+
if (matched !== null) return matched;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (Object.prototype.hasOwnProperty.call(sourceMap, '*')) {
|
|
313
|
+
return extractCeilingNumber(sourceMap['*']);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Applies custom ceiling overrides to a cloned catalog object.
|
|
321
|
+
* @param {object} catalog
|
|
322
|
+
* @param {object | null} customCeilings
|
|
323
|
+
* @returns {object}
|
|
324
|
+
*/
|
|
325
|
+
function applyCeilingOverrides(catalog, customCeilings) {
|
|
326
|
+
if (!customCeilings || typeof customCeilings !== 'object') {
|
|
327
|
+
return catalog;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const sourceMap =
|
|
331
|
+
customCeilings.models && typeof customCeilings.models === 'object'
|
|
332
|
+
? customCeilings.models
|
|
333
|
+
: customCeilings.ceilings && typeof customCeilings.ceilings === 'object'
|
|
334
|
+
? customCeilings.ceilings
|
|
335
|
+
: customCeilings;
|
|
336
|
+
|
|
337
|
+
// Global wildcard override if present
|
|
338
|
+
if (typeof sourceMap['*'] === 'number' && sourceMap['*'] > 0) {
|
|
339
|
+
for (const modelSpec of Object.values(catalog.models)) {
|
|
340
|
+
modelSpec.contextCeiling = sourceMap['*'];
|
|
341
|
+
}
|
|
342
|
+
} else if (sourceMap['*'] && typeof sourceMap['*'] === 'object') {
|
|
343
|
+
for (const modelSpec of Object.values(catalog.models)) {
|
|
344
|
+
if (typeof sourceMap['*'].contextCeiling === 'number') {
|
|
345
|
+
modelSpec.contextCeiling = sourceMap['*'].contextCeiling;
|
|
346
|
+
}
|
|
347
|
+
if (typeof sourceMap['*'].operationalSoftCeiling === 'number') {
|
|
348
|
+
modelSpec.operationalSoftCeiling = sourceMap['*'].operationalSoftCeiling;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
for (const [rawKey, overrideVal] of Object.entries(sourceMap)) {
|
|
354
|
+
if (rawKey === '*' || rawKey === 'schemaVersion') continue;
|
|
355
|
+
const normId = catalog.models[rawKey] ? rawKey : normalizeModelId(rawKey);
|
|
356
|
+
const target = catalog.models[normId];
|
|
357
|
+
if (!target) continue;
|
|
358
|
+
|
|
359
|
+
if (typeof overrideVal === 'number' && overrideVal > 0) {
|
|
360
|
+
target.contextCeiling = overrideVal;
|
|
361
|
+
} else if (overrideVal && typeof overrideVal === 'object') {
|
|
362
|
+
if (typeof overrideVal.contextCeiling === 'number' && overrideVal.contextCeiling > 0) {
|
|
363
|
+
target.contextCeiling = overrideVal.contextCeiling;
|
|
364
|
+
}
|
|
365
|
+
if (typeof overrideVal.ceiling === 'number' && overrideVal.ceiling > 0) {
|
|
366
|
+
target.contextCeiling = overrideVal.ceiling;
|
|
367
|
+
}
|
|
368
|
+
if (typeof overrideVal.operationalSoftCeiling === 'number' && overrideVal.operationalSoftCeiling > 0) {
|
|
369
|
+
target.operationalSoftCeiling = overrideVal.operationalSoftCeiling;
|
|
370
|
+
}
|
|
371
|
+
if (overrideVal.ratesPer1M && typeof overrideVal.ratesPer1M === 'object') {
|
|
372
|
+
target.ratesPer1M = { ...target.ratesPer1M, ...overrideVal.ratesPer1M };
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return catalog;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Loads the unified Anthropic Model Catalog and merges any user `customCeilings.json` overrides.
|
|
382
|
+
* @param {object} [options={}]
|
|
383
|
+
* @returns {object}
|
|
384
|
+
*/
|
|
385
|
+
export function loadModelCatalog(options = {}) {
|
|
386
|
+
let baseCatalog = BUILTIN_CATALOG;
|
|
387
|
+
const modDir = getModuleDir(options);
|
|
388
|
+
if (modDir) {
|
|
389
|
+
const jsonPath = path.join(modDir, 'anthropicModelCatalog.json');
|
|
390
|
+
const diskCatalog = readJsonSafe(jsonPath, null, options);
|
|
391
|
+
if (diskCatalog && diskCatalog.models) {
|
|
392
|
+
baseCatalog = diskCatalog;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const cloned = JSON.parse(JSON.stringify(baseCatalog));
|
|
397
|
+
const customCeilings = loadCustomCeilings(options);
|
|
398
|
+
return applyCeilingOverrides(cloned, customCeilings);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Retrieves the model specification for a given model ID (with safe fallback to `claude-sonnet-5-5`).
|
|
403
|
+
* Honors `[1m]` / `-1m` / `options.extendedContext1M` 1M context windows on Claude 4.6 models (`ISS-002`).
|
|
404
|
+
*
|
|
405
|
+
* @param {string} [modelId='']
|
|
406
|
+
* @param {object} [options={}]
|
|
407
|
+
* @returns {object}
|
|
408
|
+
*/
|
|
409
|
+
export function getModelSpec(modelId = '', options = {}) {
|
|
410
|
+
const catalog = options.catalog || loadModelCatalog(options);
|
|
411
|
+
const normId = normalizeModelId(modelId);
|
|
412
|
+
const spec =
|
|
413
|
+
catalog.models[normId] ||
|
|
414
|
+
catalog.models[catalog.defaultModel] ||
|
|
415
|
+
BUILTIN_CATALOG.models['claude-sonnet-5-5'];
|
|
416
|
+
|
|
417
|
+
if (
|
|
418
|
+
(normId === 'claude-opus-4-6' || normId === 'claude-sonnet-4-6') &&
|
|
419
|
+
hasExtended1MHint(modelId, options) &&
|
|
420
|
+
resolveCustomCeilingForModel(modelId, options) === null
|
|
421
|
+
) {
|
|
422
|
+
return {
|
|
423
|
+
...spec,
|
|
424
|
+
contextCeiling: 1000000
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return spec;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Extracts normalized prompt & output token counts across all flat, camelCase, snake_case,
|
|
433
|
+
* and nested Anthropic `cache_creation.ephemeral_*` usage formats without double-counting (`ISS-003`, `ISS-004`).
|
|
434
|
+
*
|
|
435
|
+
* @param {object} [usage={}]
|
|
436
|
+
* @returns {{
|
|
437
|
+
* uncachedInputTokens: number,
|
|
438
|
+
* cacheReadTokens: number,
|
|
439
|
+
* write5mTokens: number,
|
|
440
|
+
* write1hTokens: number,
|
|
441
|
+
* remainingGenericWriteTokens: number,
|
|
442
|
+
* effectiveWrite5mTokens: number,
|
|
443
|
+
* effectiveWrite1hTokens: number,
|
|
444
|
+
* totalCacheWriteTokens: number,
|
|
445
|
+
* outputTokens: number,
|
|
446
|
+
* thinkingTokens: number
|
|
447
|
+
* }}
|
|
448
|
+
*/
|
|
449
|
+
export function extractNormalizedTokenBreakdown(usage = {}) {
|
|
450
|
+
if (!usage || typeof usage !== 'object') {
|
|
451
|
+
return {
|
|
452
|
+
uncachedInputTokens: 0,
|
|
453
|
+
cacheReadTokens: 0,
|
|
454
|
+
write5mTokens: 0,
|
|
455
|
+
write1hTokens: 0,
|
|
456
|
+
remainingGenericWriteTokens: 0,
|
|
457
|
+
effectiveWrite5mTokens: 0,
|
|
458
|
+
effectiveWrite1hTokens: 0,
|
|
459
|
+
totalCacheWriteTokens: 0,
|
|
460
|
+
outputTokens: 0,
|
|
461
|
+
thinkingTokens: 0
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const uncachedInputTokens = Math.max(
|
|
466
|
+
0,
|
|
467
|
+
Number(
|
|
468
|
+
usage.uncachedInputTokens ??
|
|
469
|
+
usage.freshInputTokens ??
|
|
470
|
+
usage.inputTokens ??
|
|
471
|
+
usage.input_tokens ??
|
|
472
|
+
0
|
|
473
|
+
)
|
|
474
|
+
);
|
|
475
|
+
const cacheReadTokens = Math.max(
|
|
476
|
+
0,
|
|
477
|
+
Number(
|
|
478
|
+
usage.cacheReadTokens ??
|
|
479
|
+
usage.cacheReadInputTokens ??
|
|
480
|
+
usage.cache_read_input_tokens ??
|
|
481
|
+
0
|
|
482
|
+
)
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
const nestedCreation =
|
|
486
|
+
usage.cache_creation && typeof usage.cache_creation === 'object'
|
|
487
|
+
? usage.cache_creation
|
|
488
|
+
: null;
|
|
489
|
+
|
|
490
|
+
const write5mTokens = Math.max(
|
|
491
|
+
0,
|
|
492
|
+
Number(
|
|
493
|
+
usage.cacheWrite5mTokens ??
|
|
494
|
+
usage.cache_write_5m_tokens ??
|
|
495
|
+
nestedCreation?.ephemeral_5m_input_tokens ??
|
|
496
|
+
0
|
|
497
|
+
)
|
|
498
|
+
);
|
|
499
|
+
const write1hTokens = Math.max(
|
|
500
|
+
0,
|
|
501
|
+
Number(
|
|
502
|
+
usage.cacheWrite1hTokens ??
|
|
503
|
+
usage.cache_write_1h_tokens ??
|
|
504
|
+
nestedCreation?.ephemeral_1h_input_tokens ??
|
|
505
|
+
0
|
|
506
|
+
)
|
|
507
|
+
);
|
|
508
|
+
const genericWriteTokens = Math.max(
|
|
509
|
+
0,
|
|
510
|
+
Number(
|
|
511
|
+
usage.cacheWriteTokens ??
|
|
512
|
+
usage.cacheCreationInputTokens ??
|
|
513
|
+
usage.cache_creation_input_tokens ??
|
|
514
|
+
usage.lastWriteTokens ??
|
|
515
|
+
0
|
|
516
|
+
)
|
|
517
|
+
);
|
|
518
|
+
|
|
519
|
+
const remainingGenericWriteTokens = Math.max(
|
|
520
|
+
0,
|
|
521
|
+
genericWriteTokens - (write5mTokens + write1hTokens)
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
const defaultTier =
|
|
525
|
+
usage.cacheTtlTier === '1h' || usage.ttlTier === '1h' ? '1h' : '5m';
|
|
526
|
+
const effectiveWrite5mTokens =
|
|
527
|
+
write5mTokens + (defaultTier === '5m' ? remainingGenericWriteTokens : 0);
|
|
528
|
+
const effectiveWrite1hTokens =
|
|
529
|
+
write1hTokens + (defaultTier === '1h' ? remainingGenericWriteTokens : 0);
|
|
530
|
+
const totalCacheWriteTokens = effectiveWrite5mTokens + effectiveWrite1hTokens;
|
|
531
|
+
|
|
532
|
+
const outputTokens = Math.max(
|
|
533
|
+
0,
|
|
534
|
+
Number(usage.outputTokens ?? usage.output_tokens ?? usage.lastOutputTokens ?? 0)
|
|
535
|
+
);
|
|
536
|
+
const thinkingTokens = Math.max(
|
|
537
|
+
0,
|
|
538
|
+
Number(
|
|
539
|
+
usage.thinkingTokens ??
|
|
540
|
+
usage.thinking_tokens ??
|
|
541
|
+
usage.reasoning_tokens ??
|
|
542
|
+
nestedCreation?.thinking_tokens ??
|
|
543
|
+
usage.lastThinkingTokens ??
|
|
544
|
+
0
|
|
545
|
+
)
|
|
546
|
+
);
|
|
547
|
+
|
|
548
|
+
return {
|
|
549
|
+
uncachedInputTokens,
|
|
550
|
+
cacheReadTokens,
|
|
551
|
+
write5mTokens,
|
|
552
|
+
write1hTokens,
|
|
553
|
+
remainingGenericWriteTokens,
|
|
554
|
+
effectiveWrite5mTokens,
|
|
555
|
+
effectiveWrite1hTokens,
|
|
556
|
+
totalCacheWriteTokens,
|
|
557
|
+
outputTokens,
|
|
558
|
+
thinkingTokens
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Computes the exact single-turn total prompt token envelope
|
|
564
|
+
* (`uncachedInputTokens + cacheReadTokens + write5mTokens + write1hTokens + remainingGenericWriteTokens`),
|
|
565
|
+
* honoring an optional explicit single-turn `usage.turnPromptTokens` override while never inflating
|
|
566
|
+
* a single turn with multi-turn cumulative sums (`ISS-003`, `ISS-004`).
|
|
567
|
+
*
|
|
568
|
+
* @param {object} [usage={}]
|
|
569
|
+
* @returns {number}
|
|
570
|
+
*/
|
|
571
|
+
export function computeTotalPromptTokens(usage = {}) {
|
|
572
|
+
if (!usage || typeof usage !== 'object') return 0;
|
|
573
|
+
|
|
574
|
+
if (usage.turnPromptTokens !== undefined && usage.turnPromptTokens !== null) {
|
|
575
|
+
const breakdown = extractNormalizedTokenBreakdown(usage);
|
|
576
|
+
const splitSum =
|
|
577
|
+
breakdown.uncachedInputTokens +
|
|
578
|
+
breakdown.cacheReadTokens +
|
|
579
|
+
breakdown.totalCacheWriteTokens;
|
|
580
|
+
return Math.max(splitSum, Math.max(0, Number(usage.turnPromptTokens)));
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
const breakdown = extractNormalizedTokenBreakdown(usage);
|
|
584
|
+
const hasExplicitCacheRead =
|
|
585
|
+
usage.cacheReadTokens !== undefined ||
|
|
586
|
+
usage.cacheReadInputTokens !== undefined ||
|
|
587
|
+
usage.cache_read_input_tokens !== undefined;
|
|
588
|
+
const unattributedCached = !hasExplicitCacheRead
|
|
589
|
+
? Math.max(
|
|
590
|
+
0,
|
|
591
|
+
Number(usage.cachedTokens ?? usage.cached_tokens ?? 0) - breakdown.totalCacheWriteTokens
|
|
592
|
+
)
|
|
593
|
+
: 0;
|
|
594
|
+
|
|
595
|
+
const computedPromptTokens =
|
|
596
|
+
breakdown.uncachedInputTokens +
|
|
597
|
+
breakdown.cacheReadTokens +
|
|
598
|
+
breakdown.totalCacheWriteTokens +
|
|
599
|
+
unattributedCached;
|
|
600
|
+
|
|
601
|
+
const hasExplicitUncached =
|
|
602
|
+
usage.uncachedInputTokens !== undefined ||
|
|
603
|
+
usage.freshInputTokens !== undefined ||
|
|
604
|
+
usage.inputTokens !== undefined ||
|
|
605
|
+
usage.input_tokens !== undefined;
|
|
606
|
+
|
|
607
|
+
if (hasExplicitUncached && (hasExplicitCacheRead || breakdown.totalCacheWriteTokens > 0)) {
|
|
608
|
+
return computedPromptTokens;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const explicitSingleTurnTotal = Math.max(
|
|
612
|
+
0,
|
|
613
|
+
Number(
|
|
614
|
+
usage.totalPromptTokens ?? usage.totalInputTokens ?? usage.total_input_tokens ?? 0
|
|
615
|
+
)
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
return Math.max(computedPromptTokens, explicitSingleTurnTotal);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Resolves the effective USD per 1M token rate card for a model at a given prompt token volume.
|
|
623
|
+
* Enforces the Claude Haiku 5.5 100,000-token 5x price cliff across all token categories.
|
|
624
|
+
*
|
|
625
|
+
* @param {string} [modelId='claude-sonnet-5-5']
|
|
626
|
+
* @param {number} [totalPromptTokens=0]
|
|
627
|
+
* @param {object} [options={}]
|
|
628
|
+
* @returns {{
|
|
629
|
+
* modelId: string,
|
|
630
|
+
* label: string,
|
|
631
|
+
* shortLabel: string,
|
|
632
|
+
* family: string,
|
|
633
|
+
* contextCeiling: number,
|
|
634
|
+
* operationalSoftCeiling: number,
|
|
635
|
+
* inputPer1M: number,
|
|
636
|
+
* outputPer1M: number,
|
|
637
|
+
* write5mPer1M: number,
|
|
638
|
+
* write1hPer1M: number,
|
|
639
|
+
* readPer1M: number,
|
|
640
|
+
* multipliers: { write5m: number, write1h: number, read: number },
|
|
641
|
+
* cliffTriggered: boolean,
|
|
642
|
+
* preCliffWarningTriggered: boolean,
|
|
643
|
+
* cliffThresholdTokens: number | null,
|
|
644
|
+
* cliffMultiplier: number
|
|
645
|
+
* }}
|
|
646
|
+
*/
|
|
647
|
+
export function resolveEffectiveRates(modelId = 'claude-sonnet-5-5', totalPromptTokens = 0, options = {}) {
|
|
648
|
+
const spec = getModelSpec(modelId, options);
|
|
649
|
+
const promptTokens = Math.max(0, Number(totalPromptTokens || 0));
|
|
650
|
+
|
|
651
|
+
const cliff = spec.pricingCliff || null;
|
|
652
|
+
const cliffTriggered = Boolean(cliff && promptTokens > cliff.thresholdTokens);
|
|
653
|
+
const preCliffWarningTriggered = Boolean(
|
|
654
|
+
cliff && promptTokens >= (cliff.preCliffWarningTokens || cliff.thresholdTokens * 0.9)
|
|
655
|
+
);
|
|
656
|
+
|
|
657
|
+
const activeRates = cliffTriggered && cliff.ratesPer1MAboveCliff
|
|
658
|
+
? cliff.ratesPer1MAboveCliff
|
|
659
|
+
: spec.ratesPer1M;
|
|
660
|
+
|
|
661
|
+
return {
|
|
662
|
+
modelId: spec.id,
|
|
663
|
+
label: spec.label,
|
|
664
|
+
shortLabel: spec.shortLabel || spec.label,
|
|
665
|
+
family: spec.family,
|
|
666
|
+
contextCeiling: spec.contextCeiling,
|
|
667
|
+
operationalSoftCeiling: spec.operationalSoftCeiling,
|
|
668
|
+
inputPer1M: activeRates.input,
|
|
669
|
+
outputPer1M: activeRates.output,
|
|
670
|
+
write5mPer1M: activeRates.write5m,
|
|
671
|
+
write1hPer1M: activeRates.write1h,
|
|
672
|
+
readPer1M: activeRates.read,
|
|
673
|
+
multipliers: { ...spec.multipliers },
|
|
674
|
+
cliffTriggered,
|
|
675
|
+
preCliffWarningTriggered,
|
|
676
|
+
cliffThresholdTokens: cliff ? cliff.thresholdTokens : null,
|
|
677
|
+
cliffMultiplier: cliffTriggered ? cliff.multiplier : 1.0
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Computes exact USD turn cost and uncached equivalent baseline for a single turn.
|
|
683
|
+
*
|
|
684
|
+
* @param {object} [usage={}]
|
|
685
|
+
* @param {string} [usage.model='claude-sonnet-5-5']
|
|
686
|
+
* @param {number} [usage.inputTokens=0] - Uncached input tokens
|
|
687
|
+
* @param {number} [usage.outputTokens=0] - Output tokens
|
|
688
|
+
* @param {number} [usage.thinkingTokens=0] - Extended thinking tokens (billed at output rate)
|
|
689
|
+
* @param {number} [usage.cacheWriteTokens=0] - Cache write tokens (uses cacheTtlTier if 5m/1h not split)
|
|
690
|
+
* @param {number} [usage.cacheWrite5mTokens=0] - Explicit 5-minute cache write tokens
|
|
691
|
+
* @param {number} [usage.cacheWrite1hTokens=0] - Explicit 1-hour cache write tokens
|
|
692
|
+
* @param {number} [usage.cacheReadTokens=0] - Cache read tokens
|
|
693
|
+
* @param {number} [usage.turnPromptTokens] - Optional explicit single-turn prompt size for cliff evaluation
|
|
694
|
+
* @param {number} [usage.totalPromptTokens] - Optional explicit single-turn prompt size for cliff evaluation
|
|
695
|
+
* @param {'5m'|'1h'} [usage.cacheTtlTier='5m'] - Default TTL tier when cacheWriteTokens is unsplit
|
|
696
|
+
* @param {object} [options={}]
|
|
697
|
+
*/
|
|
698
|
+
export function calculateTurnCostUSD(usage = {}, options = {}) {
|
|
699
|
+
const breakdown = extractNormalizedTokenBreakdown(usage);
|
|
700
|
+
const uncachedInput = breakdown.uncachedInputTokens;
|
|
701
|
+
const outputTokens = breakdown.outputTokens;
|
|
702
|
+
const thinkingTokens = breakdown.thinkingTokens;
|
|
703
|
+
const cacheReadTokens = breakdown.cacheReadTokens;
|
|
704
|
+
const write5mTokens = breakdown.effectiveWrite5mTokens;
|
|
705
|
+
const write1hTokens = breakdown.effectiveWrite1hTokens;
|
|
706
|
+
const totalCacheWriteTokens = breakdown.totalCacheWriteTokens;
|
|
707
|
+
const totalPromptTokens = computeTotalPromptTokens(usage);
|
|
708
|
+
|
|
709
|
+
const rawModel = usage.model || usage.modelId || 'claude-sonnet-5-5';
|
|
710
|
+
const rates = resolveEffectiveRates(rawModel, totalPromptTokens, options);
|
|
711
|
+
|
|
712
|
+
const inputCostUSD = (uncachedInput / 1_000_000) * rates.inputPer1M;
|
|
713
|
+
const write5mCostUSD = (write5mTokens / 1_000_000) * rates.write5mPer1M;
|
|
714
|
+
const write1hCostUSD = (write1hTokens / 1_000_000) * rates.write1hPer1M;
|
|
715
|
+
const readCostUSD = (cacheReadTokens / 1_000_000) * rates.readPer1M;
|
|
716
|
+
const outputCostUSD = ((outputTokens + thinkingTokens) / 1_000_000) * rates.outputPer1M;
|
|
717
|
+
|
|
718
|
+
const actualCacheSpendUSD = write5mCostUSD + write1hCostUSD + readCostUSD;
|
|
719
|
+
const uncachedCacheEquivalentUSD = ((totalCacheWriteTokens + cacheReadTokens) / 1_000_000) * rates.inputPer1M;
|
|
720
|
+
|
|
721
|
+
const totalActualSpendUSD = inputCostUSD + actualCacheSpendUSD + outputCostUSD;
|
|
722
|
+
const totalUncachedEquivalentUSD = inputCostUSD + uncachedCacheEquivalentUSD + outputCostUSD;
|
|
723
|
+
const netCacheSavingsUSD = totalUncachedEquivalentUSD - totalActualSpendUSD;
|
|
724
|
+
|
|
725
|
+
return {
|
|
726
|
+
modelId: rates.modelId,
|
|
727
|
+
rates,
|
|
728
|
+
tokens: {
|
|
729
|
+
uncachedInput,
|
|
730
|
+
cacheWrite5m: write5mTokens,
|
|
731
|
+
cacheWrite1h: write1hTokens,
|
|
732
|
+
totalCacheWrite: totalCacheWriteTokens,
|
|
733
|
+
cacheRead: cacheReadTokens,
|
|
734
|
+
totalPrompt: totalPromptTokens,
|
|
735
|
+
output: outputTokens,
|
|
736
|
+
thinking: thinkingTokens,
|
|
737
|
+
totalOutput: outputTokens + thinkingTokens
|
|
738
|
+
},
|
|
739
|
+
costsUSD: {
|
|
740
|
+
uncachedInput: Number(inputCostUSD.toFixed(6)),
|
|
741
|
+
write5m: Number(write5mCostUSD.toFixed(6)),
|
|
742
|
+
write1h: Number(write1hCostUSD.toFixed(6)),
|
|
743
|
+
cacheRead: Number(readCostUSD.toFixed(6)),
|
|
744
|
+
output: Number(outputCostUSD.toFixed(6)),
|
|
745
|
+
actualCacheSpendUSD: Number(actualCacheSpendUSD.toFixed(6)),
|
|
746
|
+
uncachedCacheEquivalentUSD: Number(uncachedCacheEquivalentUSD.toFixed(6)),
|
|
747
|
+
totalActualSpendUSD: Number(totalActualSpendUSD.toFixed(6)),
|
|
748
|
+
totalUncachedEquivalentUSD: Number(totalUncachedEquivalentUSD.toFixed(6)),
|
|
749
|
+
netCacheSavingsUSD: Number(netCacheSavingsUSD.toFixed(6))
|
|
750
|
+
},
|
|
751
|
+
cliffTriggered: rates.cliffTriggered,
|
|
752
|
+
preCliffWarningTriggered: rates.preCliffWarningTriggered
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
export default {
|
|
757
|
+
BUILTIN_CATALOG,
|
|
758
|
+
hasExtended1MHint,
|
|
759
|
+
normalizeModelId,
|
|
760
|
+
loadCustomCeilings,
|
|
761
|
+
resolveCustomCeilingForModel,
|
|
762
|
+
loadModelCatalog,
|
|
763
|
+
getModelSpec,
|
|
764
|
+
extractNormalizedTokenBreakdown,
|
|
765
|
+
computeTotalPromptTokens,
|
|
766
|
+
resolveEffectiveRates,
|
|
767
|
+
calculateTurnCostUSD
|
|
768
|
+
};
|