Task #6: Cost-based work fee pricing with BTC oracle
## 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
This commit is contained in:
@@ -40,13 +40,42 @@ export const JobState = {
|
||||
failed: "failed",
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Cost breakdown shown with the work invoice (estimations at invoice-creation time)
|
||||
*/
|
||||
export interface PricingBreakdown {
|
||||
/** Total estimated cost in USD (token cost + DO infra + margin) */
|
||||
estimatedCostUsd?: number;
|
||||
/** Originator margin percentage applied */
|
||||
marginPct?: number;
|
||||
/** BTC/USD spot price used to convert the invoice to sats */
|
||||
btcPriceUsd?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actual cost record stored after the job completes
|
||||
*/
|
||||
export interface CostLedger {
|
||||
actualInputTokens?: number;
|
||||
actualOutputTokens?: number;
|
||||
/** Sum of actualInputTokens + actualOutputTokens */
|
||||
totalTokens?: number;
|
||||
/** Raw Anthropic token cost (no infra, no margin) */
|
||||
actualCostUsd?: number;
|
||||
estimatedCostUsd?: number;
|
||||
marginPct?: number;
|
||||
btcPriceUsd?: number;
|
||||
}
|
||||
|
||||
export interface JobStatusResponse {
|
||||
jobId: string;
|
||||
state: JobState;
|
||||
evalInvoice?: InvoiceInfo;
|
||||
workInvoice?: InvoiceInfo;
|
||||
pricingBreakdown?: PricingBreakdown;
|
||||
reason?: string;
|
||||
result?: string;
|
||||
costLedger?: CostLedger;
|
||||
errorMessage?: string;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user