Sonnet 5.5 as the default AI model, and a model list with prices (1.6.1)
Settings -> AI picks the model from a list (utils/aiModels.js: Sonnet, Haiku, Opus 5.5 with list prices) or takes another model ID; choosing one fills in its prices. Blank prices use the built-in ones, and cache reads are metered at each model's own price. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "events-hub-backend",
|
||||
"version": "1.6.0",
|
||||
"version": "1.6.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "events-hub-backend",
|
||||
"version": "1.6.0",
|
||||
"version": "1.6.1",
|
||||
"hasInstallScript": true,
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^1.3.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "events-hub-backend",
|
||||
"version": "1.6.0",
|
||||
"version": "1.6.1",
|
||||
"private": true,
|
||||
"description": "Cross Events Hub — API for managing Cross Events installs, licences and heartbeats",
|
||||
"main": "src/index.js",
|
||||
|
||||
@@ -14,6 +14,7 @@ const { VERSION_RE, sortReleases, syncLatestVersion } = require('../utils/releas
|
||||
const { emailNotice } = require('../utils/notices');
|
||||
const { parseRepoUrl, syncReleases } = require('../utils/releaseSync');
|
||||
const ai = require('../utils/ai');
|
||||
const { AI_MODELS } = require('../utils/aiModels');
|
||||
|
||||
const router = express.Router();
|
||||
router.use(protect);
|
||||
@@ -384,7 +385,7 @@ router.delete('/notices/:id', wrap(async (req, res) => {
|
||||
// ─── Settings ─────────────────────────────────────────────────────────────────
|
||||
|
||||
router.get('/settings', wrap(async (req, res) => {
|
||||
res.json({ settings: await getSettings(), publicKey: licenceToken.keyPair().publicKeyPem, smtp: await smtpConfigured() });
|
||||
res.json({ settings: await getSettings(), publicKey: licenceToken.keyPair().publicKeyPem, smtp: await smtpConfigured(), aiModels: AI_MODELS });
|
||||
}));
|
||||
|
||||
const SMTP_FIELDS = ['smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_pass_clear', 'smtp_from'];
|
||||
|
||||
+12
-5
@@ -15,10 +15,12 @@
|
||||
const Anthropic = require('@anthropic-ai/sdk');
|
||||
const prisma = require('../config/db');
|
||||
const { getSettings, aiApiKey } = require('./settings');
|
||||
const { findModel } = require('./aiModels');
|
||||
|
||||
// South African time (UTC+2, no daylight saving): the month the cap belongs to.
|
||||
const SAST_MS = 2 * 60 * 60 * 1000;
|
||||
// Cache writes cost 1.25× the input price, cache reads 0.1×.
|
||||
// Cache writes cost 1.25× the input price; reads are priced per model (utils/aiModels.js), 0.1×
|
||||
// the input price for a model the Hub doesn't know.
|
||||
const CACHE_WRITE = 1.25;
|
||||
const CACHE_READ = 0.1;
|
||||
// Prompts are priced higher from 100K tokens, so the Hub refuses anything close to it.
|
||||
@@ -46,22 +48,27 @@ function renewsOn(date = new Date()) {
|
||||
|
||||
/**
|
||||
* Prices for a call, or null when the Hub's AI isn't set up (no key or no exchange rate).
|
||||
* Blank prices use the built-in ones for the model (utils/aiModels.js); cache reads always do,
|
||||
* falling back to 0.1× the input price for a model the Hub doesn't know.
|
||||
* `licence.aiMarkupPercent` overrides the Hub's default markup.
|
||||
*/
|
||||
function pricing(settings, licence, hasKey) {
|
||||
const inUsd = parseFloat(settings.ai_input_usd_per_mtok);
|
||||
const outUsd = parseFloat(settings.ai_output_usd_per_mtok);
|
||||
const known = findModel(settings.ai_model);
|
||||
const price = (value, builtIn) => (value != null && String(value).trim() !== '' ? parseFloat(value) : builtIn ?? NaN);
|
||||
const inUsd = price(settings.ai_input_usd_per_mtok, known?.inputUsd);
|
||||
const outUsd = price(settings.ai_output_usd_per_mtok, known?.outputUsd);
|
||||
const usdZar = parseFloat(settings.ai_usd_zar);
|
||||
const markup = licence?.aiMarkupPercent ?? parseInt(settings.ai_markup_percent, 10);
|
||||
if (!hasKey || !settings.ai_model || !(inUsd >= 0) || !(outUsd > 0) || !(usdZar > 0) || !(markup >= 0)) return null;
|
||||
return { model: settings.ai_model, inUsd, outUsd, usdZar, markup };
|
||||
const cacheReadUsd = known && known.inputUsd === inUsd ? known.cacheReadUsd : inUsd * CACHE_READ;
|
||||
return { model: settings.ai_model, inUsd, outUsd, cacheReadUsd, usdZar, markup };
|
||||
}
|
||||
|
||||
/** Real and charged cost of some usage, in millicents (rounded up). */
|
||||
function costOf(usage, price) {
|
||||
const usd = ((usage.inputTokens || 0) * price.inUsd
|
||||
+ (usage.cacheWriteTokens || 0) * price.inUsd * CACHE_WRITE
|
||||
+ (usage.cacheReadTokens || 0) * price.inUsd * CACHE_READ
|
||||
+ (usage.cacheReadTokens || 0) * (price.cacheReadUsd ?? price.inUsd * CACHE_READ)
|
||||
+ (usage.outputTokens || 0) * price.outUsd) / 1e6;
|
||||
const real = usd * price.usdZar * 100 * 1000;
|
||||
return { real: Math.ceil(real), charged: Math.ceil(real * (1 + price.markup / 100)) };
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* The Claude models Settings → AI offers, with Anthropic's list prices in US dollars per million
|
||||
* tokens (prompts under 100K tokens — the Hub refuses anything near that). Anthropic's API doesn't
|
||||
* publish prices, so they're kept here: choosing a model fills in its prices, which stay editable
|
||||
* in case Anthropic changes them before a Hub release does.
|
||||
*
|
||||
* Prices as of October 2026. Cache writes (5-minute) cost 1.25× the input price for every model;
|
||||
* cache reads vary, so each model gives its own.
|
||||
*/
|
||||
|
||||
const AI_MODELS = [
|
||||
{ id: 'claude-sonnet-5-5', label: 'Claude Sonnet 5.5', inputUsd: 2, outputUsd: 10, cacheReadUsd: 0.2, note: 'The default. Chosen after the October 2026 trial: right on report figures, feedback counts and message spelling.' },
|
||||
{ id: 'claude-haiku-5-5', label: 'Claude Haiku 5.5', inputUsd: 0.1, outputUsd: 0.5, cacheReadUsd: 0.01, note: 'About 15× cheaper; made some factual and spelling mistakes in the trial.' },
|
||||
{ id: 'claude-opus-5-5', label: 'Claude Opus 5.5', inputUsd: 4, outputUsd: 20, cacheReadUsd: 0.2, note: 'The most capable of the three, at twice Sonnet\'s price.' },
|
||||
];
|
||||
|
||||
const DEFAULT_AI_MODEL = AI_MODELS[0];
|
||||
|
||||
const findModel = id => AI_MODELS.find(m => m.id === id) || null;
|
||||
|
||||
module.exports = { AI_MODELS, DEFAULT_AI_MODEL, findModel };
|
||||
@@ -1,5 +1,6 @@
|
||||
const prisma = require('../config/db');
|
||||
const { encrypt, decrypt } = require('./crypto');
|
||||
const { DEFAULT_AI_MODEL } = require('./aiModels');
|
||||
|
||||
// Hub settings Joshua edits on the Settings page, with their defaults.
|
||||
const DEFAULTS = {
|
||||
@@ -26,10 +27,12 @@ const DEFAULTS = {
|
||||
release_sync_error: '',
|
||||
// AI for Cross Events 2.0 (utils/ai.js). Off until a key and the exchange rate are set.
|
||||
ai_api_key: '',
|
||||
ai_model: 'claude-haiku-5-5',
|
||||
// One of utils/aiModels.js, or another model ID typed in.
|
||||
ai_model: DEFAULT_AI_MODEL.id,
|
||||
// US dollars per million tokens, for prompts under 100K tokens (the Hub refuses bigger ones).
|
||||
ai_input_usd_per_mtok: '0.10',
|
||||
ai_output_usd_per_mtok: '0.50',
|
||||
// Filled in from utils/aiModels.js when a model is chosen; blank means the built-in price.
|
||||
ai_input_usd_per_mtok: String(DEFAULT_AI_MODEL.inputUsd),
|
||||
ai_output_usd_per_mtok: String(DEFAULT_AI_MODEL.outputUsd),
|
||||
// Rand per US dollar.
|
||||
ai_usd_zar: '',
|
||||
// Default markup on the real cost, in %; a licence can override it.
|
||||
|
||||
@@ -38,7 +38,7 @@ jest.mock('@anthropic-ai/sdk', () => {
|
||||
const ai = require('../src/utils/ai');
|
||||
|
||||
const install = { id: 'inst_1', licenceId: 'lic_1' };
|
||||
const price = { model: 'claude-haiku-5-5', inUsd: 0.1, outUsd: 0.5, usdZar: 20, markup: 40 };
|
||||
const price = { model: 'claude-haiku-5-5', inUsd: 0.1, outUsd: 0.5, cacheReadUsd: 0.01, usdZar: 20, markup: 40 };
|
||||
const body = { protocolVersion: 1, helper: 'draft_message', system: 'Write a friendly message.', input: 'Bring a sleeping bag.', maxTokens: 1000 };
|
||||
const reply = (usage, extra = {}) => ({ model: 'claude-haiku-5-5', stop_reason: 'end_turn', content: [{ type: 'text', text: 'Hi all!' }], usage, ...extra });
|
||||
|
||||
@@ -67,6 +67,27 @@ describe('pricing', () => {
|
||||
expect(ai.pricing(mockSettings, { aiMarkupPercent: 0 }, true).markup).toBe(0);
|
||||
});
|
||||
|
||||
test('Sonnet 5.5 is the default model, at its list prices', () => {
|
||||
const { DEFAULTS } = jest.requireActual('../src/utils/settings');
|
||||
expect(DEFAULTS).toMatchObject({ ai_model: 'claude-sonnet-5-5', ai_input_usd_per_mtok: '2', ai_output_usd_per_mtok: '10' });
|
||||
});
|
||||
|
||||
test('blank prices use the built-in list; cache reads are priced per model', () => {
|
||||
const sonnet = ai.pricing({ ...mockSettings, ai_model: 'claude-sonnet-5-5', ai_input_usd_per_mtok: '', ai_output_usd_per_mtok: '' }, mockState.licence, true);
|
||||
expect(sonnet).toMatchObject({ inUsd: 2, outUsd: 10, cacheReadUsd: 0.2 });
|
||||
// Opus 5.5 reads its cache at 0.05× the input price.
|
||||
expect(ai.pricing({ ...mockSettings, ai_model: 'claude-opus-5-5', ai_input_usd_per_mtok: '4', ai_output_usd_per_mtok: '20' }, mockState.licence, true).cacheReadUsd).toBe(0.2);
|
||||
});
|
||||
|
||||
test('a model the Hub doesn\'t know needs its prices; cache reads at 0.1× of an edited price', () => {
|
||||
expect(ai.pricing({ ...mockSettings, ai_model: 'claude-new-model', ai_input_usd_per_mtok: '', ai_output_usd_per_mtok: '' }, mockState.licence, true)).toBeNull();
|
||||
const other = ai.pricing({ ...mockSettings, ai_model: 'claude-new-model', ai_input_usd_per_mtok: '3', ai_output_usd_per_mtok: '15' }, mockState.licence, true);
|
||||
expect(other.inUsd).toBe(3);
|
||||
expect(other.cacheReadUsd).toBeCloseTo(0.3);
|
||||
// Sonnet with a changed input price: its listed cache price no longer applies.
|
||||
expect(ai.pricing({ ...mockSettings, ai_model: 'claude-sonnet-5-5', ai_input_usd_per_mtok: '2.5', ai_output_usd_per_mtok: '10' }, mockState.licence, true).cacheReadUsd).toBe(0.25);
|
||||
});
|
||||
|
||||
test('cost in millicents: input, cache writes at 1.25×, reads at 0.1×, output; markup on top', () => {
|
||||
// 1M input tokens at $0.10 × R20 = R2 = 200 000 millicents.
|
||||
expect(ai.costOf({ inputTokens: 1e6 }, price)).toEqual({ real: 200000, charged: 280000 });
|
||||
|
||||
Reference in New Issue
Block a user