@kognai/clawrouter-x402 0.1.1 → 0.1.3

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.
@@ -288,11 +288,24 @@ async function callAnthropicDirect(model, messages, maxTokens = 4096) {
288
288
  throw new Error(`Anthropic direct API returned ${res.status}: ${res.data.slice(0, 300)}`);
289
289
  }
290
290
  const json = JSON.parse(res.data);
291
+ const inTok = json.usage?.input_tokens || 0;
292
+ const outTok = json.usage?.output_tokens || 0;
293
+ // TICKET-350 cost visibility: the direct path used to hardcode cost_usd=0, so
294
+ // the honest-routing haiku calls logged $0 and the ledger under-counted real
295
+ // spend. Compute the actual cost from token usage × per-million USD rates.
296
+ const DIRECT_RATES = {
297
+ 'claude-haiku-4-5': { in: 0.80, out: 4.00 },
298
+ 'claude-sonnet-4': { in: 3.00, out: 15.00 },
299
+ 'claude-opus-4': { in: 15.00, out: 75.00 },
300
+ };
301
+ const rateKey = Object.keys(DIRECT_RATES).find(k => model.includes(k));
302
+ const rate = rateKey ? DIRECT_RATES[rateKey] : { in: 0.80, out: 4.00 }; // default ≈ haiku
303
+ const cost_usd = (inTok * rate.in + outTok * rate.out) / 1_000_000;
291
304
  return {
292
305
  content: json.content?.[0]?.text || '',
293
- input_tokens: json.usage?.input_tokens || 0,
294
- output_tokens: json.usage?.output_tokens || 0,
295
- cost_usd: 0, // billing handled outside (no gateway metering on direct path)
306
+ input_tokens: inTok,
307
+ output_tokens: outTok,
308
+ cost_usd,
296
309
  };
297
310
  }
298
311
  // ── x402 Payment Signing (EIP-3009 TransferWithAuthorization) ─────────────────
@@ -723,6 +736,32 @@ async function routeCall(req) {
723
736
  output_tokens = result.output_tokens;
724
737
  cost_usd = 0; // local = free
725
738
  }
739
+ else if (route.tier === 'T2.5') {
740
+ // TICKET-350 honest routing: routine cloud work (the 'exec' tier) runs a
741
+ // CHEAP+FAST model DIRECTLY — bypassing the OpenClaw gateway, which flattened
742
+ // EVERY model to openclaw/main=opus AND had a cold ~12min first call. So
743
+ // routine code is now genuinely cheap + fast (and the cost_usd recorded is
744
+ // the REAL model price, not the gateway's). Hard/escalated tasks (T3/apex)
745
+ // still go through the gateway → opus. Falls back to the gateway if the
746
+ // direct call errors (e.g. missing key) so routing never hard-fails.
747
+ const cheapModel = process.env.KOGNAI_EXEC_MODEL || 'claude-haiku-4-5-20251001';
748
+ try {
749
+ const result = await callAnthropicDirect(cheapModel, messages, payload.max_tokens || 4096);
750
+ content = result.content;
751
+ input_tokens = result.input_tokens;
752
+ output_tokens = result.output_tokens;
753
+ cost_usd = result.cost_usd;
754
+ route = { tier: 'T2.5-EXEC', model: cheapModel, local: false };
755
+ }
756
+ catch (e) {
757
+ console.warn(`[ClawRouter] direct exec call failed (${e?.message || e}) — falling back to gateway`);
758
+ const result = await callCloudGateway(route.model, messages, payload.max_tokens || 4096);
759
+ content = result.content;
760
+ input_tokens = result.input_tokens;
761
+ output_tokens = result.output_tokens;
762
+ cost_usd = result.cost_usd;
763
+ }
764
+ }
726
765
  else {
727
766
  const result = await callCloudGateway(route.model, messages, payload.max_tokens || 4096);
728
767
  content = result.content;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kognai/clawrouter-x402",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "description": "ClawRouter v2 — Kognai's viem-backed x402 ModelRouter implementation. Injected into @kognai/orchestrator-core's router seam at startup, so core never depends on viem; products that don't route on-chain simply don't install this package.",
6
6
  "main": "dist/index.js",