## New files - btc-oracle.ts: CoinGecko BTC/USD fetch (60s cache), usdToSats() helper (ceil, min 1 sat), 5s abort timeout, fallback to BTC_PRICE_USD_FALLBACK env var (default $100k) - lib/db/migrations/0002_cost_based_pricing.sql: SQL migration artifact adding 6 new columns to jobs table (estimated_cost_usd, margin_pct, btc_price_usd, actual_input_tokens, actual_output_tokens, actual_cost_usd); idempotent via ADD COLUMN IF NOT EXISTS ## Modified files - pricing.ts: Full rewrite — per-model token rates (Haiku/Sonnet, env-var overridable), DO infra amortisation per request, originator margin %, estimateInputTokens/Output by tier, calculateActualCostUsd() for post-work ledger, async calculateWorkFeeSats() → WorkFeeBreakdown - agent.ts: WorkResult now includes inputTokens + outputTokens from Anthropic usage; workModel/evalModel exposed as readonly public; EVAL_MODEL/WORK_MODEL env var support - lib/db/src/schema/jobs.ts: 6 new real/integer columns; schema pushed to DB - jobs.ts route: Work invoice creation calls pricingService.calculateWorkFeeSats() async; stores estimatedCostUsd/marginPct/btcPriceUsd; post-work stores actualInputTokens/ actualOutputTokens/actualCostUsd; GET response includes pricingBreakdown and costLedger with totalTokens (input + output computed field) - openapi.yaml: PricingBreakdown + CostLedger schemas (with totalTokens) added - lib/api-zod/src/generated/api.ts: Regenerated with new schemas - lib/api-client-react/src/generated/api.schemas.ts: Regenerated (PricingBreakdown, CostLedger) - replit.md: 17 new env vars documented in cost-based pricing section
18 lines
1.2 KiB
SQL
18 lines
1.2 KiB
SQL
-- Migration: Add cost-based pricing columns to jobs table
|
|
-- Task #6: Cost-based work fee pricing with BTC oracle
|
|
|
|
ALTER TABLE jobs
|
|
ADD COLUMN IF NOT EXISTS estimated_cost_usd REAL,
|
|
ADD COLUMN IF NOT EXISTS margin_pct REAL,
|
|
ADD COLUMN IF NOT EXISTS btc_price_usd REAL,
|
|
ADD COLUMN IF NOT EXISTS actual_input_tokens INTEGER,
|
|
ADD COLUMN IF NOT EXISTS actual_output_tokens INTEGER,
|
|
ADD COLUMN IF NOT EXISTS actual_cost_usd REAL;
|
|
|
|
COMMENT ON COLUMN jobs.estimated_cost_usd IS 'Total estimated cost in USD at work invoice creation time (token + DO infra + margin)';
|
|
COMMENT ON COLUMN jobs.margin_pct IS 'Originator margin percentage applied to this job';
|
|
COMMENT ON COLUMN jobs.btc_price_usd IS 'BTC/USD price used to convert work fee to sats (from CoinGecko at invoice time)';
|
|
COMMENT ON COLUMN jobs.actual_input_tokens IS 'Actual input token count reported by Anthropic after work execution';
|
|
COMMENT ON COLUMN jobs.actual_output_tokens IS 'Actual output token count reported by Anthropic after work execution';
|
|
COMMENT ON COLUMN jobs.actual_cost_usd IS 'Actual raw Anthropic token cost (no infra amortisation, no margin) after work execution';
|