@bridgenode/llm 0.1.0 → 0.2.1

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/README.md CHANGED
@@ -1,1442 +1,82 @@
1
- # @blockrun/llm (TypeScript SDK)
1
+ # @bridgenode/llm (TypeScript SDK)
2
2
 
3
- > **@blockrun/llm** is a TypeScript/Node.js SDK for accessing <!-- br:models.chatVisible -->66<!-- /br:models.chatVisible --> large language models (GPT-5, Claude, Gemini, Grok, DeepSeek, Kimi, and more) with automatic pay-per-request USDC micropayments via the x402 protocol. No API keys required — your wallet signature is your authentication. Supports **streaming**, smart routing, Base and Solana chains.
4
- >
5
- > 🆓 **Includes 7 fully-free NVIDIA-hosted models** (5 visible in `/v1/models`, 2 hidden but directly callable) — DeepSeek V4 Flash (1M context), Nemotron Nano Omni (vision), Qwen3 Coder, Llama 4, Mistral, plus the gpt-oss pair. Zero USDC, no rate-limit gimmicks. Use `routingProfile: 'free'` or call any `nvidia/*` model directly.
3
+ > **@bridgenode/llm** TypeScript/Node.js SDK for BridgeNode: pay-per-request AI inference via x402 micropayments on Solana. No API keys required — your wallet signature is your authentication.
6
4
 
7
- [![npm](https://img.shields.io/npm/v/@blockrun/llm.svg)](https://www.npmjs.com/package/@blockrun/llm)
8
- [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
5
+ **Protocol:** x402 v2
6
+ **Payment:** USDC (Solana SPL)
7
+ **Gasless:** Solana fee sponsorship — agent needs 0 SOL
9
8
 
10
- ## Supported Chains
9
+ ---
11
10
 
12
- | Chain | Network | Payment | Status |
13
- |-------|---------|---------|--------|
14
- | **Base** | Base Mainnet (Chain ID: 8453) | USDC | Primary |
15
- | **Base Testnet** | Base Sepolia (Chain ID: 84532) | Testnet USDC | Development |
16
- | **Solana** | Solana Mainnet | USDC (SPL) | New |
11
+ ## Supported Models (3)
17
12
 
13
+ | Model | Type | Use case |
14
+ |-------|------|----------|
15
+ | `deepseek-v4-flash` | Chat | Default — fast, 1M context, 284B MoE |
16
+ | `groq-llama-3.3-70b` | Chat | Eco tier — cheapest, fastest |
17
+ | `deepseek-v4-pro` | Chat | Premium — complex reasoning |
18
18
 
19
- **Protocol:** x402 v2 (CDP Facilitator)
19
+ *More models added as BridgeNode expands.*
20
+
21
+ ---
20
22
 
21
23
  ## Installation
22
24
 
23
25
  ```bash
24
- # Base and Solana support (optional Solana deps auto-installed)
25
- npm install @blockrun/llm
26
- # or
27
- pnpm add @blockrun/llm
26
+ npm install @bridgenode/llm
28
27
  # or
29
- yarn add @blockrun/llm
30
- ```
31
-
32
- ## Quick Start (Base - Default)
33
-
34
- ```typescript
35
- import { LLMClient } from '@blockrun/llm';
36
-
37
- const client = new LLMClient(); // Uses BASE_CHAIN_WALLET_KEY (never sent to server)
38
- const response = await client.chat('openai/gpt-4o', 'Hello!');
39
- ```
40
-
41
- That's it. The SDK handles x402 payment automatically.
42
-
43
- ## `BlockrunClient` — the universal primitive (recommended for new code)
44
-
45
- Starting in `2.5.0`, the SDK ships a single `BlockrunClient` that speaks to
46
- **every** BlockRun endpoint over x402. New API surfaces are intended to be
47
- distributed as [Claude Code skills](https://github.com/anthropics/skills)
48
- that drive this primitive — no SDK release required to add an endpoint.
49
-
50
- ```typescript
51
- import { BlockrunClient } from '@blockrun/llm';
52
-
53
- const br = new BlockrunClient();
54
-
55
- // Sync GET — Surf market price (Tier 1, $0.001)
56
- const btc = await br.get('/v1/surf/market/price', { symbol: 'BTC' });
57
-
58
- // Sync POST — raw on-chain SQL (Tier 3, $0.020)
59
- const rows = await br.post('/v1/surf/onchain/sql', {
60
- query: 'SELECT block_number FROM ethereum.blocks ORDER BY block_number DESC LIMIT 1',
61
- });
62
-
63
- // Submit + poll — long-running video gen (settled only on completion)
64
- const video = await br.poll('/v1/videos/generations', {
65
- model: 'xai/grok-imagine-video',
66
- prompt: 'a red apple spinning',
67
- });
68
-
69
- // Streaming SSE — chat completions
70
- for await (const chunk of br.stream('/v1/chat/completions', {
71
- model: 'anthropic/claude-sonnet-4-6',
72
- messages: [{ role: 'user', content: 'Hi' }],
73
- stream: true,
74
- })) {
75
- process.stdout.write(chunk?.choices?.[0]?.delta?.content ?? '');
76
- }
77
- ```
78
-
79
- Four call shapes cover every endpoint type:
80
- - `get<T>(path, params?)` — synchronous GET (price, ranking, list, news)
81
- - `post<T>(path, body?)` — synchronous POST (on-chain SQL, search)
82
- - `poll<T>(path, body?, { budgetMs, intervalMs })` — submit + poll (image, video, music, voice)
83
- - `stream<T>(path, body?)` — async iterator over SSE chunks (chat)
84
-
85
- The per-API client classes (`LLMClient`, `ImageClient`, `VideoClient`,
86
- `PortraitClient`, `VoiceClient`, `MusicClient`, `SearchClient`, `RpcClient`,
87
- `PriceClient`, `SurfClient`) all remain — they will be soft-deprecated in 2.6 (rewritten as
88
- shims over `BlockrunClient`) and removed in 3.0.
89
-
90
- ### Try It Free (No USDC Required)
91
-
92
- Want to kick the tires before funding a wallet? Route to BlockRun's free NVIDIA tier:
93
-
94
- ```typescript
95
- import { LLMClient } from '@blockrun/llm';
96
-
97
- const client = new LLMClient(); // Wallet still required for signing, but $0 charged
98
-
99
- // Option 1: call a free model directly
100
- const reply = await client.chat('nvidia/deepseek-v4-flash', 'Explain x402 in 1 sentence');
101
-
102
- // Option 2: let the smart router pick the best free model per request
103
- const result = await client.smartChat('What is 2+2?', { routingProfile: 'free' });
104
- console.log(result.model); // e.g. 'nvidia/deepseek-v4-flash' (cheapest capable for SIMPLE tier)
105
- console.log(result.response); // '4'
28
+ pnpm add @bridgenode/llm
106
29
  ```
107
30
 
108
- **Available free models** (input + output both $0, all NVIDIA-hosted, last refreshed 2026-06-07):
109
-
110
- | Model ID | Context | Best For |
111
- |----------|---------|----------|
112
- | `nvidia/deepseek-v4-flash` | 1M | DeepSeek V4 Flash — 284B / 13B active MoE, ~5× faster than V4 Pro. Best free chat / summarization / light reasoning |
113
- | `nvidia/nemotron-3-nano-omni-30b-a3b-reasoning` | 256K | Only vision-capable free model — text + images + video (≤2 min) + audio (≤1 hr) |
114
- | `nvidia/llama-4-maverick` | 131K | Meta Llama 4 Maverick MoE |
115
- | `nvidia/qwen3-coder-480b` | 131K | Coding-optimised 480B MoE |
116
- | `nvidia/mistral-small-4-119b` | 131K | ⚠️ Upstream timing out as of 2026-06-07 — avoid until NVIDIA recovers it |
117
- | `nvidia/gpt-oss-120b` | 128K | OpenAI open-weight 120B — 123 tok/s. Hidden from `/v1/models` for privacy but direct calls still work |
118
- | `nvidia/gpt-oss-20b` | 128K | OpenAI open-weight 20B — 155 tok/s. Hidden from `/v1/models` but direct calls still work |
119
-
120
- > Need V4-Pro-class reasoning? Use the paid `deepseek/deepseek-v4-pro` ($0.435/$0.87 — the 75% launch promo became the permanent list price after 2026-05-31) — `nvidia/deepseek-v4-pro` is currently hidden because NVIDIA's NIM deployment is hung; backend MODEL_REDIRECTS forwards calls to V4 Flash.
121
-
122
- > Privacy note: `nvidia/gpt-oss-120b` and `nvidia/gpt-oss-20b` are hidden from `/v1/models` because NVIDIA's free build.nvidia.com tier reserves the right to use prompts/outputs for service improvement. Direct calls by full model ID still work — opt in only when your data isn't sensitive.
123
-
124
- > Retired: `nvidia/qwen3-next-80b-a3b-thinking` hit NVIDIA end-of-life 2026-05-21 (HTTP 410). The gateway auto-redirects pinned callers to `nvidia/llama-4-maverick`.
125
-
126
- ## Quick Start (Solana)
31
+ ## Quick Start
127
32
 
128
33
  ```typescript
129
- import { SolanaLLMClient } from '@blockrun/llm';
130
-
131
- // SOLANA_WALLET_KEY env var (bs58-encoded Solana secret key)
132
- const client = new SolanaLLMClient();
133
- const response = await client.chat('openai/gpt-4o', 'gm Solana');
134
- console.log(response);
135
- ```
136
-
137
- Set `SOLANA_WALLET_KEY` to your bs58-encoded Solana secret key. Payments are automatic via x402 — your key never leaves your machine.
138
-
139
- ## Solana Support
140
-
141
- Pay for AI calls with Solana USDC via [sol.blockrun.ai](https://sol.bridgenode.cc):
142
-
143
- ```typescript
144
- import { SolanaLLMClient } from '@blockrun/llm';
145
-
146
- // SOLANA_WALLET_KEY env var (bs58-encoded Solana secret key)
147
- const client = new SolanaLLMClient();
148
-
149
- // Or pass key directly
150
- const client2 = new SolanaLLMClient({ privateKey: 'your-bs58-solana-key' });
151
-
152
- // Same API as LLMClient
153
- const response = await client.chat('openai/gpt-4o', 'gm Solana');
154
- console.log(response);
155
-
156
- // Live Search with Grok (Solana payment)
157
- const tweet = await client.chat('xai/grok-3-mini', 'What is trending on X?', { search: true });
158
- ```
159
-
160
- **Setup:**
161
- 1. Export your Solana wallet key: `export SOLANA_WALLET_KEY="your-bs58-key"`
162
- 2. Fund with USDC on Solana mainnet
163
- 3. That's it — payments are automatic via x402
164
-
165
- **Supported endpoint:** `https://sol.bridgenode.cc/api`
166
- **Payment:** Solana USDC (SPL, mainnet)
167
-
168
- ## How Payment Works
169
-
170
- No API keys, no subscription. You hold USDC in your own wallet, and **every request pays for itself** with an on-chain micropayment. Two phases:
171
-
172
- ### Phase 1 — Fund your wallet once
173
-
174
- You only do this when your balance runs low. Three ways to get USDC into your wallet:
175
-
176
- - **(a) Buy with a card (Base USDC).** Call the new `onramp()` method to mint a one-time Coinbase Onramp link, then open the returned `pay.coinbase.com` URL — pay by card/bank in 60+ fiat currencies and the USDC lands in your wallet. The call itself is **free**. Onramp is **Base-only** (buying USDC with a card always lands Base USDC), and the funding address must equal your signing wallet:
177
-
178
- ```typescript
179
- const { url } = await client.onramp(client.getWalletAddress());
180
- console.log(`Fund your wallet: ${url}`); // single-use, expires ~5 min — mint at click time
181
- ```
182
-
183
- - **(b) Transfer existing USDC.** Send USDC you already hold to your wallet address (`client.getWalletAddress()`). On Base, send Base USDC; on Solana (`SolanaLLMClient`), send Solana SPL USDC.
184
-
185
- - **(c) Skip funding entirely.** Use the free NVIDIA models (e.g. `nvidia/deepseek-v4-flash`) — every call is **$0**, no balance required.
186
-
187
- $5 of USDC covers thousands of paid requests. Check your balance any time:
188
-
189
- ```typescript
190
- const balance = await client.getBalance(); // USDC on Base
191
- console.log(`Balance: $${balance.toFixed(2)} USDC`);
192
- ```
193
-
194
- ### Phase 2 — Every request pays itself (automatic x402)
195
-
196
- You just call e.g. `client.chat(...)` — the payment is invisible:
197
-
198
- 1. You send a request to BlockRun's API.
199
- 2. The gateway returns **402 Payment Required** with the price.
200
- 3. The SDK signs a USDC payment **locally** (EIP-712) — on **Base** for `LLMClient`, on **Solana** for `SolanaLLMClient` — using your wallet key.
201
- 4. The request is retried automatically with the payment proof.
202
- 5. The gateway settles on-chain and returns the AI response.
203
-
204
- One call, no separate pay step. Free NVIDIA models settle at **$0** (no payment signed).
205
-
206
- ### Track spend and verify settlements
207
-
208
- ```typescript
209
- import { getCostSummary } from '@blockrun/llm';
210
-
211
- const spent = client.getSpending(); // this session
212
- console.log(`Spent $${spent.totalUsd.toFixed(4)} across ${spent.calls} calls`);
213
-
214
- const summary = getCostSummary(); // across sessions (~/.blockrun/data/costs.jsonl)
215
- console.log(`Lifetime: $${summary.totalUsd.toFixed(2)} over ${summary.calls} calls`);
216
- ```
217
-
218
- Every paid request is a real on-chain USDC transfer — look up your wallet address on [Basescan](https://basescan.org) (or a Solana explorer) to verify each settlement independently.
219
-
220
- **Non-custodial by design: your private key never leaves your machine** — it is only used for local signing, and no funds are ever held by BlockRun.
221
-
222
- ## Smart Routing (ClawRouter)
223
-
224
- Let the SDK automatically pick the cheapest capable model for each request:
225
-
226
- ```typescript
227
- import { LLMClient } from '@blockrun/llm';
228
-
229
- const client = new LLMClient();
230
-
231
- // Auto-routes to cheapest capable model
232
- const result = await client.smartChat('What is 2+2?');
233
- console.log(result.response); // '4'
234
- console.log(result.model); // 'moonshot/kimi-k2.5' (cheap, fast)
235
- console.log(`Saved ${(result.routing.savings * 100).toFixed(0)}%`); // 'Saved 87%'
236
-
237
- // Complex reasoning task -> routes to reasoning model
238
- const complex = await client.smartChat('Prove the Riemann hypothesis step by step');
239
- console.log(complex.model); // 'xai/grok-4-1-fast-reasoning'
240
-
241
- // Inspect the fallback chain SmartChat will walk on transient errors.
242
- console.log(complex.routing.fallbacks); // ['anthropic/claude-opus-4.7', ...]
243
- ```
244
-
245
- ### Automatic Fallback on Transient Errors
246
-
247
- `smartChat()` populates a tier-specific fallback chain and `chat()` /
248
- `chatCompletion()` walk it automatically when the primary model returns a
249
- transient error — timeouts, network failures, or 5xx responses (502/503/504/
250
- 522/524). 4xx errors and `PaymentError` propagate immediately so wallet /
251
- auth issues surface fast.
252
-
253
- ```typescript
254
- // Manually pass a fallback chain to chat() / chatCompletion()
255
- const reply = await client.chat('nvidia/deepseek-v4-flash', 'hello', {
256
- fallbackModels: ['nvidia/llama-4-maverick', 'nvidia/mistral-small-4-119b'],
257
- });
258
- // If deepseek-v4-flash times out, the SDK retries against the next model
259
- // and logs each hop to stderr: "[@blockrun/llm] <from> -> <to> (...)".
260
- ```
261
-
262
- ### Routing Profiles
263
-
264
- | Profile | Description | Best For |
265
- |---------|-------------|----------|
266
- | `free` | NVIDIA free tier — smart-routes across <!-- br:models.free -->8<!-- /br:models.free --> models (DeepSeek V4 Flash, Nemotron Nano Omni, Qwen3, Llama 4, Mistral, plus 2 hidden gpt-oss) | Zero-cost testing, dev, prod |
267
- | `eco` | Cheapest models per tier (DeepSeek, xAI) | Cost-sensitive production |
268
- | `auto` | Best balance of cost/quality (default) | General use |
269
- | `premium` | Top-tier models (OpenAI, Anthropic) | Quality-critical tasks |
270
-
271
- ```typescript
272
- // Use premium models for complex tasks
273
- const result = await client.smartChat(
274
- 'Write production-grade async TypeScript code',
275
- { routingProfile: 'premium' }
276
- );
277
- console.log(result.model); // 'anthropic/claude-opus-4.7'
278
- ```
279
-
280
- ### How ClawRouter Works
281
-
282
- ClawRouter uses a 14-dimension rule-based classifier to analyze each request:
283
-
284
- - **Token count** - Short vs long prompts
285
- - **Code presence** - Programming keywords
286
- - **Reasoning markers** - "prove", "step by step", etc.
287
- - **Technical terms** - Architecture, optimization, etc.
288
- - **Creative markers** - Story, poem, brainstorm, etc.
289
- - **Agentic patterns** - Multi-step, tool use indicators
290
-
291
- The classifier runs in <1ms, 100% locally, and routes to one of four tiers:
292
-
293
- | Tier | Example Tasks | Auto Profile Model |
294
- |------|---------------|-------------------|
295
- | SIMPLE | "What is 2+2?", definitions | moonshot/kimi-k2.5 |
296
- | MEDIUM | Code snippets, explanations | xai/grok-code-fast-1 |
297
- | COMPLEX | Architecture, long documents | google/gemini-3.1-pro |
298
- | REASONING | Proofs, multi-step reasoning | xai/grok-4-1-fast-reasoning |
299
-
300
- ## Available Models
301
-
302
- ### OpenAI GPT-5.5 Family
303
- Released 2026-04-23 — first fully retrained base since GPT-4.5. 1M context, 128K output, native agent + computer use.
304
-
305
- | Model | Input Price | Output Price |
306
- |-------|-------------|--------------|
307
- | `openai/gpt-5.5` | $5.00/M | $30.00/M |
308
-
309
- ### OpenAI GPT-5.4 Family
310
- | Model | Input Price | Output Price |
311
- |-------|-------------|--------------|
312
- | `openai/gpt-5.4` | $2.50/M | $15.00/M |
313
- | `openai/gpt-5.4-pro` | $30.00/M | $180.00/M |
314
- | `openai/gpt-5.4-nano` | $0.20/M | $1.25/M |
315
-
316
- ### OpenAI GPT-5 Family
317
- | Model | Input Price | Output Price |
318
- |-------|-------------|--------------|
319
- | `openai/gpt-5.3` | $1.75/M | $14.00/M |
320
- | `openai/gpt-5.2` | $1.75/M | $14.00/M |
321
- | `openai/gpt-5-mini` | $0.25/M | $2.00/M |
322
- | `openai/gpt-5.2-pro` | $21.00/M | $168.00/M |
323
- | `openai/gpt-5.2-codex` | $1.75/M | $14.00/M |
324
-
325
- ### OpenAI GPT-4 Family
326
- | Model | Input Price | Output Price |
327
- |-------|-------------|--------------|
328
- | `openai/gpt-4.1` | $2.00/M | $8.00/M |
329
- | `openai/gpt-4.1-mini` | $0.40/M | $1.60/M |
330
- | `openai/gpt-4.1-nano` | $0.10/M | $0.40/M |
331
- | `openai/gpt-4o` | $2.50/M | $10.00/M |
332
- | `openai/gpt-4o-mini` | $0.15/M | $0.60/M |
333
-
334
- ### OpenAI O-Series (Reasoning)
335
- | Model | Input Price | Output Price |
336
- |-------|-------------|--------------|
337
- | `openai/o1` | $15.00/M | $60.00/M |
338
- | `openai/o3` | $2.00/M | $8.00/M |
339
- | `openai/o3-mini` | $1.10/M | $4.40/M |
340
- | `openai/o4-mini` | $1.10/M | $4.40/M |
341
-
342
- ### Anthropic Claude
343
- | Model | Input Price | Output Price | Context | Notes |
344
- |-------|-------------|--------------|---------|-------|
345
- | `anthropic/claude-fable-5` | $10.00/M | $50.00/M | **1M** | Mythos-class flagship above Opus — always-on thinking, 128K output, fallback `claude-opus-4.8`. Alias: `claude-fable-5` |
346
- | `anthropic/claude-opus-4.8` | $5.00/M | $25.00/M | **1M** | Flagship — agentic coding + adaptive thinking, 128K output |
347
- | `anthropic/claude-opus-4.7` | $5.00/M | $25.00/M | **1M** | Agentic coding + adaptive thinking, 128K output |
348
- | `anthropic/claude-opus-4.6` | $5.00/M | $25.00/M | 200K | Hidden but still callable — kept as in-family hot-swap fallback |
349
- | `anthropic/claude-opus-4.5` | $5.00/M | $25.00/M | 200K | |
350
- | `anthropic/claude-opus-4` | $15.00/M | $75.00/M | 200K | |
351
- | `anthropic/claude-sonnet-4.6` | $3.00/M | $15.00/M | 200K | Best for reasoning/instructions |
352
- | `anthropic/claude-sonnet-4` | $3.00/M | $15.00/M | 200K | |
353
- | `anthropic/claude-haiku-4.5` | $1.00/M | $5.00/M | 200K | |
354
-
355
- ### Google Gemini
356
- | Model | Input Price | Output Price |
357
- |-------|-------------|--------------|
358
- | `google/gemini-3.1-pro` | $2.00/M | $12.00/M |
359
- | `google/gemini-3.5-flash` | $0.50/M | $3.00/M |
360
- | `google/gemini-3.1-flash-lite` | $0.25/M | $1.50/M |
361
- | `google/gemini-3-flash-preview` | $0.50/M | $3.00/M |
362
- | `google/gemini-2.5-pro` | $1.25/M | $10.00/M |
363
- | `google/gemini-2.5-flash` | $0.30/M | $2.50/M |
364
- | `google/gemini-2.5-flash-lite` | $0.10/M | $0.40/M |
365
-
366
- ### DeepSeek
367
-
368
- V4 family launched 2026-04-24. DeepSeek upstream now serves the legacy
369
- `deepseek-chat` / `deepseek-reasoner` aliases as V4 Flash non-thinking /
370
- thinking modes. V4 Pro is the new flagship paid SKU — 1.6T MoE / 49B active,
371
- 1M context, MMLU-Pro 87.5, GPQA 90.1, SWE-bench 80.6, LiveCodeBench 93.5.
372
-
373
- | Model | Input Price | Output Price | Context | Notes |
374
- |-------|-------------|--------------|---------|-------|
375
- | `deepseek/deepseek-v4-pro` | $0.435/M | $0.87/M | 1M | V4 flagship — strongest open-weight reasoner. The 75% launch promo became the permanent list price after 2026-05-31 |
376
- | `deepseek/deepseek-chat` | $0.20/M | $0.40/M | 1M | V4 Flash non-thinking (paid endpoint with 5MB request bodies; same upstream as `nvidia/deepseek-v4-flash`) |
377
- | `deepseek/deepseek-reasoner` | $0.20/M | $0.40/M | 1M | V4 Flash thinking (same upstream as `deepseek-chat`, thinking enabled by default) |
378
-
379
- ### xAI Grok
380
-
381
- Grok 4.3 and Grok Build are resold through BlockRun's OpenRouter credit pool
382
- (same pattern as `deepseek/deepseek-v4-pro` and `minimax/minimax-m3`). The
383
- older Grok chat SKUs (grok-3/3-mini, grok-4-fast / 4-1-fast families,
384
- grok-code-fast-1, grok-4-0709, grok-2-vision) are now **hidden from
385
- `/v1/models`** — direct calls by full ID still work, but SmartChat won't
386
- auto-pick them.
34
+ import { LLMClient } from "@bridgenode/llm";
387
35
 
388
- | Model | Input Price | Output Price | Context | Notes |
389
- |-------|-------------|--------------|---------|-------|
390
- | `xai/grok-4.3` | $1.50/M | $4.00/M | 1M | Reasoning model, vision-capable, tuned for agentic workflows |
391
- | `xai/grok-build-0.1` | $1.50/M | $3.00/M | 256K | Fast agentic coding model — interactive software-engineering workflows |
392
-
393
- ### Moonshot Kimi
394
- | Model | Input Price | Output Price |
395
- |-------|-------------|--------------|
396
- | `moonshot/kimi-k2.6` | $0.95/M | $4.00/M |
397
- | `moonshot/kimi-k2.5` | $0.60/M | $3.00/M |
398
-
399
- ### MiniMax
400
- | Model | Input Price | Output Price |
401
- |-------|-------------|--------------|
402
- | `minimax/minimax-m3` | $0.30/M | $1.20/M |
403
- | `minimax/minimax-m2.7` | $0.30/M | $1.20/M |
404
-
405
- ### NVIDIA (Free) + Moonshot
406
-
407
- Free tier refreshed 2026-04-28: added `nvidia/deepseek-v4-flash` (1M context)
408
- and Nemotron Nano Omni (vision). `nvidia/gpt-oss-120b` and
409
- `nvidia/gpt-oss-20b` were briefly delisted over privacy concerns then
410
- **re-enabled 2026-04-30** with `available: true` + `hidden: true` — they
411
- no longer appear in `/v1/models` (so SmartChat won't auto-pick them) but
412
- direct calls by full ID still return HTTP 200. `nvidia/deepseek-v4-pro`,
413
- `nvidia/deepseek-v3.2`, and `nvidia/glm-4.7` are hidden because NVIDIA's
414
- NIM deployment is hung — backend MODEL_REDIRECTS forwards calls to V4
415
- Flash / qwen3-coder. `nvidia/qwen3-next-80b-a3b-thinking` hit NVIDIA
416
- end-of-life 2026-05-21 (HTTP 410) and is auto-redirected to
417
- `nvidia/llama-4-maverick`.
418
-
419
- | Model | Input Price | Output Price | Notes |
420
- |-------|-------------|--------------|-------|
421
- | `nvidia/deepseek-v4-flash` | **FREE** | **FREE** | 284B / 13B active MoE, 1M context — best free chat / summarization / light reasoning |
422
- | `nvidia/nemotron-3-nano-omni-30b-a3b-reasoning` | **FREE** | **FREE** | 31B / 3.2B active MoE, 256K — only vision-capable free model |
423
- | `nvidia/mistral-small-4-119b` | **FREE** | **FREE** | ⚠️ Upstream timing out as of 2026-06-07 |
424
- | `nvidia/llama-4-maverick` | **FREE** | **FREE** | Meta Llama 4 Maverick MoE |
425
- | `nvidia/qwen3-coder-480b` | **FREE** | **FREE** | Coding-optimised 480B MoE |
426
- | `nvidia/gpt-oss-120b` | **FREE** | **FREE** | Hidden from `/v1/models` for privacy but direct calls still work — 123 tok/s |
427
- | `nvidia/gpt-oss-20b` | **FREE** | **FREE** | Hidden from `/v1/models` but direct calls still work — 155 tok/s |
428
- | `moonshot/kimi-k2.5` | $0.60/M | $3.00/M | Direct from Moonshot — replaces `nvidia/kimi-k2.5` |
429
-
430
- ### E2E Verified Models
431
-
432
- All models below have been tested end-to-end via the TypeScript SDK (Feb 2026):
433
-
434
- | Provider | Model | Status |
435
- |----------|-------|--------|
436
- | OpenAI | `openai/gpt-4o-mini` | Passed |
437
- | OpenAI | `openai/gpt-5.2-codex` | Passed |
438
- | Anthropic | `anthropic/claude-opus-4.6` | Passed |
439
- | Anthropic | `anthropic/claude-sonnet-4` | Passed |
440
- | Google | `google/gemini-2.5-flash` | Passed |
441
- | DeepSeek | `deepseek/deepseek-chat` | Passed |
442
- | xAI | `xai/grok-3` | Passed |
443
- | Moonshot | `moonshot/kimi-k2.6` | Passed |
444
-
445
- ### Image Generation
446
- | Model | Price |
447
- |-------|-------|
448
- | `openai/dall-e-3` | $0.04-0.08/image |
449
- | `openai/gpt-image-1` | $0.02-0.04/image |
450
- | `openai/gpt-image-2` | $0.06-0.12/image (reasoning-driven, multilingual text rendering, character consistency) |
451
- | `google/nano-banana` | $0.05/image |
452
- | `google/nano-banana-pro` | $0.10-0.15/image |
453
- | `xai/grok-imagine-image` | $0.02/image |
454
- | `xai/grok-imagine-image-pro` | $0.07/image |
455
- | `zai/cogview-4` | $0.015/image |
456
-
457
- Image editing (`client.edit`) via `/v1/images/image2image`: `openai/gpt-image-1`, `openai/gpt-image-2`, `google/nano-banana`, and `google/nano-banana-pro`. Pass a single base64 `data:image/...` URI to edit one image, or an array of 2–4 URIs to **fuse** them (e.g. a subject + a brand logo). Fusion caps: `openai/*` up to 4 source images, `google/*` up to 3. A `mask` cannot be combined with multiple source images.
458
-
459
- ```ts
460
- // Multi-image fusion with Nano Banana
461
- const fused = await client.edit(
462
- "Place the logo on the t-shirt",
463
- [subjectDataUri, logoDataUri],
464
- { model: "google/nano-banana" }
465
- );
466
- console.log(fused.data[0].url);
467
- ```
468
-
469
- ### Video Generation
470
- | Model | Price |
471
- |-------|-------|
472
- | `xai/grok-imagine-video` | $0.05/sec (8s default → $0.42/clip) |
473
- | `bytedance/seedance-1.5-pro` | $0.03/sec (5s default, up to 10s, 720p) |
474
- | `bytedance/seedance-2.0-fast` | $0.15/sec (~60-80s gen, sweet-spot price/quality) |
475
- | `bytedance/seedance-2.0` | $0.30/sec (720p Pro) |
476
-
477
- ```ts
478
- import { VideoClient } from '@blockrun/llm';
479
-
480
- const client = new VideoClient();
481
- const result = await client.generate('a red apple slowly spinning on a wooden table');
482
- console.log(result.data[0].url); // permanent MP4 URL
483
- console.log(result.data[0].duration_seconds); // 8
484
-
485
- // Image-to-video
486
- const r2 = await client.generate('the subject turns and smiles', {
487
- imageUrl: 'https://example.com/portrait.jpg',
488
- });
489
-
490
- // Token360 / Seedance options (silently ignored by xAI Grok video)
491
- const r3 = await client.generate('aerial drone shot over a snowy mountain', {
492
- model: 'bytedance/seedance-2.0-fast',
493
- aspectRatio: '21:9',
494
- resolution: '1080p',
495
- generateAudio: true, // omit to use the model's default
496
- seed: 42,
497
- watermark: false,
498
- returnLastFrame: true, // useful for clip chaining
499
- });
500
-
501
- // First-and-last-frame interpolation (Seedance only): the model tweens
502
- // from imageUrl (first frame) to lastFrameUrl (final frame).
503
- // Priced identically to image-to-video.
504
- const r4 = await client.generate('the flower blooms in golden morning light', {
505
- model: 'bytedance/seedance-1.5-pro',
506
- imageUrl: 'https://example.com/bud.jpg',
507
- lastFrameUrl: 'https://example.com/bloom.jpg',
508
- });
509
-
510
- // Omni / multi-reference (Seedance 2.0 only): up to 9 reference images
511
- // for character/style consistency. Cite them as "image 1", "image 2" in
512
- // the prompt. Mutually exclusive with imageUrl / lastFrameUrl /
513
- // realFaceAssetId.
514
- const r5 = await client.generate(
515
- 'the character from image 1 walks through the city from image 2',
516
- {
517
- model: 'bytedance/seedance-2.0',
518
- referenceImageUrls: [
519
- 'https://example.com/character.jpg',
520
- 'https://example.com/city.jpg',
521
- ],
522
- }
523
- );
524
- ```
525
-
526
- ### Text-to-Speech & Sound Effects
527
-
528
- `SpeechClient` wraps BlockRun Voice (ElevenLabs): `POST /v1/audio/speech`
529
- (OpenAI-compatible TTS), `POST /v1/audio/sound-effects`, and the free
530
- `GET /v1/audio/voices`. TTS price scales with character count:
531
- `(chars / 1000) × model rate`, minimum $0.001/request. Synthesis is
532
- synchronous (<1s for Flash).
533
-
534
- | Model | Price | Max Input | Notes |
535
- |-------|-------|-----------|-------|
536
- | `elevenlabs/flash-v2.5` | $0.05/1k chars | 40k chars | ~75ms latency, 32 languages (default) |
537
- | `elevenlabs/turbo-v2.5` | $0.05/1k chars | 40k chars | ~250ms latency, balanced quality |
538
- | `elevenlabs/multilingual-v2` | $0.10/1k chars | 10k chars | Long-form narration, audiobooks |
539
- | `elevenlabs/v3` | $0.10/1k chars | 5k chars | Max expressiveness, 70+ languages |
540
- | `elevenlabs/sound-effects` | $0.05/generation | 1k chars | Sound effects up to 22s |
541
-
542
- ```ts
543
- import { SpeechClient } from '@blockrun/llm';
544
-
545
- const client = new SpeechClient();
546
-
547
- // Text-to-speech (voice aliases: sarah, george, laura, charlie,
548
- // river, roger, callum, harry — or any raw ElevenLabs voice_id)
549
- const result = await client.generate('Welcome to BlockRun.', { voice: 'george' });
550
- console.log(result.data[0].url); // audio URL (mp3 by default)
551
-
552
- // Other formats / speed
553
- const wav = await client.generate('Breaking news from the world of micropayments.', {
554
- model: 'elevenlabs/v3',
555
- responseFormat: 'wav',
556
- speed: 1.1,
557
- });
558
-
559
- // Sound effects (flat $0.05/generation)
560
- const fx = await client.soundEffect('rain on a tin roof, distant thunder');
561
-
562
- // List voices (free, rate-limited)
563
- const voices = await client.listVoices();
564
- ```
565
-
566
- ### Virtual Portraits
567
-
568
- `PortraitClient` wraps `POST /v1/portrait/enroll` (paid, flat **$0.01** promo,
569
- no KYC). Enroll a face image by URL and get back a Token360 asset id (`ta_xxxxxx`).
570
- Pass that id as `realFaceAssetId` on a Seedance 2.0 video generation to keep the
571
- same AI character across clips. Payment settles only after Token360 confirms the
572
- enrollment, so a failed enrollment never charges your wallet. The returned
573
- `image_url` is a gateway-mirrored copy of your source image (see `mirrored` /
574
- `source_image_url`). (Real-person likeness is not supported on BlockRun —
575
- enrolled portraits are AI characters.)
576
-
577
- ```ts
578
- import { PortraitClient, VideoClient } from '@blockrun/llm';
579
-
580
- const portraits = new PortraitClient();
581
- const { asset_id } = await portraits.enroll({
582
- name: 'Spokesperson',
583
- imageUrl: 'https://example.com/face.jpg', // public https JPG/PNG/WEBP, ≤10 MB
584
- });
585
-
586
- // Reuse the same character across Seedance 2.0 clips
587
- const video = new VideoClient();
588
- const clip = await video.generate('she waves and smiles', {
589
- model: 'bytedance/seedance-2.0-fast',
590
- realFaceAssetId: asset_id,
591
- });
592
- console.log(clip.data[0].url);
593
- ```
594
-
595
- ### Voice Calls
596
-
597
- `VoiceClient` wraps `POST /v1/voice/call` (paid, $0.54/call) and
598
- `GET /v1/voice/call/{callId}` (free polling) — AI-powered outbound phone
599
- calls powered by Bland.ai. The agent dials the recipient and runs a real-time
600
- conversation based on your `task` instructions. US + Canada destinations.
601
-
602
- ```ts
603
- import { VoiceClient } from '@blockrun/llm';
604
-
605
- const client = new VoiceClient();
606
-
607
- // Initiate (paid $0.54)
608
- const result = await client.call({
609
- to: '+14155552671',
610
- task: 'You are a friendly assistant calling to confirm a 3pm dentist appointment.',
611
- voice: 'maya', // 'nat' | 'josh' | 'maya' | 'june' | 'paige' | 'derek' | 'florian'
612
- max_duration: 5, // minutes (1–30)
613
- });
614
- console.log(result.call_id);
615
-
616
- // Poll for transcript + recording (free)
617
- const status = await client.getStatus(result.call_id);
618
- console.log(status.status, status.recording_url);
619
- ```
620
-
621
- Bring your own caller-ID: pass `from: '+14155552671'` (must be a BlockRun
622
- phone number you own; buy via `/v1/phone/numbers/buy`).
623
-
624
- ### Standalone Search
625
-
626
- `SearchClient` wraps `POST /v1/search` — standalone Grok Live Search.
627
- Pricing: `$0.025/source + margin` (10 sources ≈ `$0.26`).
628
-
629
- ```ts
630
- import { SearchClient } from '@blockrun/llm';
631
-
632
- const client = new SearchClient();
633
- const result = await client.search('Latest news on x402 adoption', {
634
- sources: ['x', 'web'],
635
- maxResults: 10,
636
- });
637
- console.log(result.summary);
638
- for (const url of result.citations ?? []) console.log(url);
639
- ```
640
-
641
- ### Surf Crypto Data
642
-
643
- `SurfClient` exposes the full `/v1/surf/*` catalog — 84+ pay-per-call
644
- endpoints across CEX/DEX market data, on-chain SQL, wallet intelligence,
645
- prediction markets (Polymarket + Kalshi), social analytics, news, VC fund
646
- data, and an OpenAI-compatible chat surface. Flat pricing per call:
647
-
648
- | Tier | Price/call | Examples |
649
- |------|-----------|----------|
650
- | 1 | $0.001 | `/market/price`, `/market/ranking`, `/news/feed`, prediction-market reads, social tweets |
651
- | 2 | $0.005 | `/exchange/depth`, `/exchange/klines`, `/wallet/detail`, `/search/*`, `/social/ranking` |
652
- | 3 | $0.020 | `/onchain/sql`, `/onchain/query`, `/onchain/schema`, `/chat/completions` |
653
-
654
- Because the catalog is broad and evolving, the client deliberately ships a
655
- generic `get` / `post` pair instead of 84 typed wrappers. Pass the path
656
- (with or without the `/v1/surf` prefix), query params, or a JSON body —
657
- type the response via a generic if you want.
658
-
659
- ```ts
660
- import { SurfClient } from '@blockrun/llm';
661
-
662
- const surf = new SurfClient();
663
-
664
- // Tier 1 — token price ($0.001)
665
- const btc = await surf.get('/market/price', { symbol: 'BTC' });
666
-
667
- // Tier 2 — order book depth ($0.005)
668
- const book = await surf.get('/exchange/depth', {
669
- exchange: 'binance',
670
- symbol: 'BTC-USDT',
671
- });
672
-
673
- // Tier 3 — raw on-chain SQL against 80+ ClickHouse tables ($0.020)
674
- const rows = await surf.post('/onchain/sql', {
675
- query: 'SELECT block_number FROM ethereum.blocks ORDER BY block_number DESC LIMIT 5',
676
- });
677
-
678
- // Typed response via generic
679
- type Price = { symbol: string; price: number; timestamp: string };
680
- const eth = await surf.get<Price>('/market/price', { symbol: 'ETH' });
681
- ```
682
-
683
- Full endpoint inventory: <https://bridgenode.cc/marketplace/surf>.
684
-
685
- Methods: `userLookup`, `userInfo`, `followers`, `following`, `followings`,
686
- `verifiedFollowers`, `userTweets`, `mentions`, `tweetLookup`, `tweetReplies`,
687
- `tweetThread`, `search`, `trending`, `articlesRising`.
688
-
689
- ### Market Data (Pyth)
690
-
691
- `PriceClient` wraps the Pyth-backed market-data endpoints. Crypto, FX and
692
- commodity are fully free (price + history + list); 12 global stock markets
693
- and the `usstock` legacy alias charge `$0.001` for price + history (list is
694
- always free). Pass `requireWallet: false` to construct a free-only client.
695
-
696
- ```ts
697
- import { PriceClient } from '@blockrun/llm';
698
-
699
- const p = new PriceClient({ requireWallet: false });
700
- const btc = await p.price('crypto', 'BTC-USD');
701
- const eur = await p.price('fx', 'EUR-USD');
702
-
703
- // Paid — requires a wallet
704
- const p2 = new PriceClient();
705
- const aapl = await p2.price('stocks', 'AAPL', { market: 'us' });
706
- const bars = await p2.history('stocks', 'AAPL', {
707
- market: 'us',
708
- resolution: 'D',
709
- from: 1700000000,
710
- to: 1710000000,
711
- });
712
- const symbols = await p.listSymbols('crypto', { query: 'sol', limit: 20 });
713
- ```
714
-
715
- Supported `StockMarket` values: `us, hk, jp, kr, gb, de, fr, nl, ie, lu, cn, ca`.
716
-
717
- ### DeFi Data, DEX Swaps & Cloud Compute
718
-
719
- Three passthrough families live directly on `LLMClient` / `SolanaLLMClient`:
720
-
721
- ```ts
722
- const client = new LLMClient();
723
-
724
- // DefiLlama — protocols / TVL / yields / prices ($0.005/call, prices $0.001)
725
- const protocols = await client.defiProtocols();
726
- const aave = await client.defiProtocol('aave');
727
- const prices = await client.defiPrices(['coingecko:bitcoin', 'base:0x833589...']);
728
-
729
- // 0x DEX — swap + gasless quotes (FREE; BlockRun takes an on-chain affiliate
730
- // fee on executed swaps instead of x402)
731
- const quote = await client.dexQuote({
732
- chainId: '8453', sellToken: '0x...', buyToken: '0x...',
733
- sellAmount: '1000000', taker: '0xYourWallet',
734
- });
735
- const gq = await client.dexGaslessQuote({ /* ... */ });
736
- const res = await client.dexGaslessSubmit({ trade: { /* signed eip712 */ } });
737
- const status = await client.dexGaslessStatus(res.tradeHash as string);
738
-
739
- // Modal — sandboxed compute ($0.01 create CPU / $0.05 GPU, $0.001 exec)
740
- const sb = await client.modalSandboxCreate({ image: 'python:3.11' });
741
- const out = await client.modalSandboxExec(sb.sandbox_id as string, ['python', '-c', 'print(42)']);
742
- await client.modalSandboxTerminate(sb.sandbox_id as string);
743
- ```
744
-
745
- Generic escape hatches: `client.defi(path, params)`, `client.dex(path, params, body?)`,
746
- `client.modal(path, body)`.
747
-
748
- ### Multi-chain RPC
749
-
750
- `RpcClient` wraps `POST /v1/rpc/{network}` — standard JSON-RPC 2.0 access to
751
- 40+ chains through one endpoint (Ethereum, Base, Solana, Polygon, BSC,
752
- Arbitrum, Optimism, Avalanche, Bitcoin, Sui, and more; powered by Tatum's RPC
753
- gateway). No API key, no per-chain endpoints: flat **$0.002 per call** in
754
- USDC; a JSON-RPC batch charges per element.
755
-
756
- ```ts
757
- import { RpcClient } from '@blockrun/llm';
758
-
759
- const client = new RpcClient();
760
-
761
- // EVM chains speak eth_* JSON-RPC
762
- const block = await client.call('ethereum', 'eth_blockNumber');
763
- console.log(parseInt(block.result as string, 16));
764
-
765
- const balance = await client.call('base', 'eth_getBalance', [
766
- '0x4200000000000000000000000000000000000006',
767
- 'latest',
768
- ]);
769
-
770
- // Non-EVM chains speak their native JSON-RPC
771
- const slot = await client.call('solana', 'getSlot');
772
- const tip = await client.call('bitcoin', 'getblockcount');
773
-
774
- // Batch: one payment, per-element pricing ($0.002 x N)
775
- const out = await client.batch('polygon', [
776
- { method: 'eth_blockNumber' },
777
- { method: 'eth_gasPrice' },
36
+ const client = new LLMClient({ privateKey: "0x..." });
37
+ const response = await client.chat("deepseek-v4-flash", [
38
+ { role: "user", content: "Hello!" }
778
39
  ]);
779
-
780
- console.log(block.network); // 'ethereum' (canonical key from X-Network)
781
- console.log(block.cacheHit); // true if served from the gateway's hot cache
782
- console.log(block.txHash); // x402 settlement tx
783
- ```
784
-
785
- 40 curated chains are exported as `SUPPORTED_NETWORKS`; common aliases
786
- (`eth`, `arb`, `op`, `matic`, `bnb`, `avax`, `sol`, `btc`, `xrp`, `dot`, ...)
787
- resolve server-side (`NETWORK_ALIASES`). Unknown but well-formed slugs fall
788
- through to a generic `{slug}-mainnet` gateway attempt, so new chains work
789
- without an SDK update. Hot, low-volatility reads (`eth_chainId`, mined
790
- blocks/receipts, `getTransaction`, ...) are served from a method-aware
791
- gateway cache — same price, lower latency.
792
-
793
- ### Testnet Models (Base Sepolia)
794
- | Model | Price |
795
- |-------|-------|
796
- | `openai/gpt-oss-20b` | $0.001/request |
797
- | `openai/gpt-oss-120b` | $0.002/request |
798
-
799
- *Testnet models use flat pricing (no token counting) for simplicity.*
800
-
801
- ## Standalone Search
802
-
803
- Search web, X/Twitter, and news without using a chat model:
804
-
805
- ```typescript
806
- import { LLMClient } from '@blockrun/llm';
807
-
808
- const client = new LLMClient();
809
-
810
- const result = await client.search('latest AI agent frameworks 2026');
811
- console.log(result.summary);
812
- for (const cite of result.citations ?? []) {
813
- console.log(` - ${cite}`);
814
- }
815
-
816
- // Filter by source type and date range
817
- const filtered = await client.search('BlockRun x402', {
818
- sources: ['web', 'x'],
819
- fromDate: '2026-01-01',
820
- maxResults: 5,
821
- });
822
- ```
823
-
824
- ## Image Editing (img2img)
825
-
826
- Edit existing images with text prompts:
827
-
828
- ```typescript
829
- import { LLMClient } from '@blockrun/llm';
830
-
831
- const client = new LLMClient();
832
-
833
- const result = await client.imageEdit(
834
- 'Make the sky purple and add northern lights',
835
- 'data:image/png;base64,...', // base64 or URL
836
- { model: 'openai/gpt-image-1' }
837
- );
838
- console.log(result.data[0].url);
839
40
  ```
840
41
 
841
- ## Usage Examples
42
+ The SDK handles the full x402 handshake automatically:
43
+ 1. Sends request → receives `402 PAYMENT-REQUIRED`
44
+ 2. Signs USDC transfer authorization locally (key never leaves your machine)
45
+ 3. Retries with `PAYMENT-SIGNATURE` header
46
+ 4. Returns the AI response
842
47
 
843
- ### Simple Chat
48
+ ## `BridgeNodeClient` — universal primitive
844
49
 
845
50
  ```typescript
846
- import { LLMClient } from '@blockrun/llm';
51
+ import { BridgeNodeClient } from "@bridgenode/llm";
847
52
 
848
- const client = new LLMClient(); // Uses BASE_CHAIN_WALLET_KEY (never sent to server)
53
+ const br = new BridgeNodeClient({ privateKey: "0x..." });
849
54
 
850
- const response = await client.chat('openai/gpt-4o', 'Explain quantum computing');
851
- console.log(response);
852
-
853
- // With system prompt
854
- const response2 = await client.chat('anthropic/claude-sonnet-4', 'Write a haiku', {
855
- system: 'You are a creative poet.',
55
+ // POST
56
+ const result = await br.post("/v1/chat/completions", {
57
+ model: "deepseek-v4-flash",
58
+ messages: [{ role: "user", content: "Hi" }],
856
59
  });
857
- ```
858
-
859
- ### Smart Routing (ClawRouter)
860
-
861
- Save up to <!-- br:savings.autoVsBaselinePct -->87<!-- /br:savings.autoVsBaselinePct -->% on inference costs with intelligent model routing. ClawRouter uses a <!-- br:clawrouter.dimensions -->15<!-- /br:clawrouter.dimensions -->-dimension rule-based scoring algorithm to select the cheapest model that can handle your request (<1ms, 100% local).
862
-
863
- ```typescript
864
- import { LLMClient } from '@blockrun/llm';
865
-
866
- const client = new LLMClient();
867
-
868
- // Auto-route to cheapest capable model
869
- const result = await client.smartChat('What is 2+2?');
870
- console.log(result.response); // '4'
871
- console.log(result.model); // 'google/gemini-2.5-flash'
872
- console.log(result.routing.tier); // 'SIMPLE'
873
- console.log(`Saved ${(result.routing.savings * 100).toFixed(0)}%`); // 'Saved 87%'
874
-
875
- // Routing profiles
876
- const free = await client.smartChat('Hello!', { routingProfile: 'free' }); // Zero cost
877
- const eco = await client.smartChat('Explain AI', { routingProfile: 'eco' }); // Budget optimized
878
- const auto = await client.smartChat('Code review', { routingProfile: 'auto' }); // Balanced (default)
879
- const premium = await client.smartChat('Write a legal brief', { routingProfile: 'premium' }); // Best quality
880
- ```
881
-
882
- **Routing Profiles:**
883
-
884
- | Profile | Description | Best For |
885
- |---------|-------------|----------|
886
- | `free` | NVIDIA free tier (<!-- br:models.free -->8<!-- /br:models.free --> models, smart-routed) | Zero-cost testing, dev, prod |
887
- | `eco` | Budget-optimized | Cost-sensitive workloads |
888
- | `auto` | Intelligent routing (default) | General use |
889
- | `premium` | Best quality models | Critical tasks |
890
-
891
- **Tiers:**
892
-
893
- | Tier | Example Tasks | Typical Models |
894
- |------|---------------|----------------|
895
- | SIMPLE | Greetings, math, lookups | Gemini Flash, GPT-4o-mini |
896
- | MEDIUM | Explanations, summaries | GPT-4o, Claude Sonnet |
897
- | COMPLEX | Analysis, code generation | GPT-5.2, Claude Opus |
898
- | REASONING | Multi-step logic, planning | o3, DeepSeek Reasoner |
899
-
900
- ### Full Chat Completion
901
-
902
- ```typescript
903
- import { LLMClient, type ChatMessage } from '@blockrun/llm';
904
-
905
- const client = new LLMClient(); // Uses BASE_CHAIN_WALLET_KEY (never sent to server)
906
-
907
- const messages: ChatMessage[] = [
908
- { role: 'system', content: 'You are a helpful assistant.' },
909
- { role: 'user', content: 'How do I read a file in Node.js?' },
910
- ];
911
-
912
- const result = await client.chatCompletion('openai/gpt-4o', messages);
913
- console.log(result.choices[0].message.content);
914
- ```
915
-
916
- ### Streaming
917
-
918
- Stream responses token-by-token with automatic x402 payment. Uses a **pre-auth cache** to skip the 402 round-trip on repeat calls to the same model (~200ms saved per request after the first).
919
-
920
- #### OpenAI-compatible (recommended)
921
-
922
- ```typescript
923
- import { OpenAI } from '@blockrun/llm';
924
-
925
- const client = new OpenAI({ walletKey: process.env.BASE_CHAIN_WALLET_KEY });
926
60
 
927
- const stream = await client.chat.completions.create({
928
- model: 'openai/gpt-5.4',
929
- messages: [{ role: 'user', content: 'Write a short story about AI agents' }],
61
+ // Streaming
62
+ for await (const chunk of br.stream("/v1/chat/completions", {
63
+ model: "deepseek-v4-flash",
64
+ messages: [{ role: "user", content: "Write a poem" }],
930
65
  stream: true,
931
- });
932
-
933
- for await (const chunk of stream) {
934
- process.stdout.write(chunk.choices[0]?.delta?.content || '');
935
- }
936
- ```
937
-
938
- #### Native client
939
-
940
- ```typescript
941
- import { LLMClient, type ChatMessage } from '@blockrun/llm';
942
-
943
- const client = new LLMClient();
944
-
945
- const messages: ChatMessage[] = [
946
- { role: 'user', content: 'Explain quantum computing in simple terms' },
947
- ];
948
-
949
- // Returns a raw fetch Response with SSE body
950
- const response = await client.chatCompletionStream('google/gemini-2.5-flash', messages);
951
-
952
- const reader = response.body!.getReader();
953
- const decoder = new TextDecoder();
954
-
955
- while (true) {
956
- const { done, value } = await reader.read();
957
- if (done) break;
958
-
959
- const chunk = decoder.decode(value, { stream: true });
960
- for (const line of chunk.split('\n')) {
961
- if (!line.startsWith('data: ') || line === 'data: [DONE]') continue;
962
- const data = JSON.parse(line.slice(6));
963
- process.stdout.write(data.choices?.[0]?.delta?.content || '');
964
- }
965
- }
966
- ```
967
-
968
- #### Payment + streaming flow
969
-
970
- ```
971
- First call (cache miss):
972
- 1. Send request → 402 response (BlockRun returns price)
973
- 2. Sign USDC payment locally (key never leaves machine)
974
- 3. Retry with PAYMENT-SIGNATURE header + stream: true
975
- 4. Cache payment requirements for this model (1h TTL)
976
- 5. Stream tokens as they arrive
977
-
978
- Subsequent calls (cache hit):
979
- 1. Pre-sign payment from cache — skip 402 round-trip
980
- 2. Send request with PAYMENT-SIGNATURE upfront
981
- 3. Stream tokens immediately (~200ms faster)
982
- ```
983
-
984
- ### List Available Models
985
-
986
- ```typescript
987
- import { LLMClient } from '@blockrun/llm';
988
-
989
- const client = new LLMClient(); // Uses BASE_CHAIN_WALLET_KEY (never sent to server)
990
- const models = await client.listModels();
991
-
992
- for (const model of models) {
993
- console.log(`${model.id}: $${model.inputPrice}/M input`);
994
- }
995
- ```
996
-
997
- ### Multiple Requests
998
-
999
- ```typescript
1000
- import { LLMClient } from '@blockrun/llm';
1001
-
1002
- const client = new LLMClient(); // Uses BASE_CHAIN_WALLET_KEY (never sent to server)
1003
-
1004
- const [gpt, claude, gemini] = await Promise.all([
1005
- client.chat('openai/gpt-4o', 'What is 2+2?'),
1006
- client.chat('anthropic/claude-sonnet-4', 'What is 3+3?'),
1007
- client.chat('google/gemini-2.5-flash', 'What is 4+4?'),
1008
- ]);
1009
- ```
1010
-
1011
- ## Prediction Markets (Powered by Predexon)
1012
-
1013
- Access real-time prediction market data from Polymarket, Kalshi, and Binance Futures via [Predexon](https://predexon.com). No API keys needed — pay-per-request via x402.
1014
-
1015
- ### Polymarket
1016
-
1017
- ```typescript
1018
- import { LLMClient } from '@blockrun/llm';
1019
-
1020
- const client = new LLMClient();
1021
-
1022
- // List markets with optional filters ($0.001/request)
1023
- const markets = await client.pm("polymarket/markets");
1024
- const filtered = await client.pm("polymarket/markets", { status: "active", limit: 10 });
1025
- const searched = await client.pm("polymarket/markets", { search: "bitcoin" });
1026
-
1027
- // List events ($0.001/request)
1028
- const events = await client.pm("polymarket/events");
1029
-
1030
- // Historical trades ($0.001/request)
1031
- const trades = await client.pm("polymarket/trades");
1032
-
1033
- // OHLCV candlestick data for a specific condition ($0.001/request)
1034
- const candles = await client.pm("polymarket/candlesticks/0x1234abcd...");
1035
-
1036
- // Wallet profile ($0.005/request — tier 2)
1037
- const profile = await client.pm("polymarket/wallet/0xABC123...");
1038
-
1039
- // Wallet P&L ($0.005/request — tier 2)
1040
- const pnl = await client.pm("polymarket/wallet/pnl/0xABC123...");
1041
-
1042
- // Global leaderboard ($0.001/request)
1043
- const leaderboard = await client.pm("polymarket/leaderboard");
1044
- ```
1045
-
1046
- ### Kalshi & Binance
1047
-
1048
- ```typescript
1049
- // Kalshi markets ($0.001/request)
1050
- const kalshiMarkets = await client.pm("kalshi/markets");
1051
-
1052
- // Kalshi trades ($0.001/request)
1053
- const kalshiTrades = await client.pm("kalshi/trades");
1054
-
1055
- // Binance candles for supported pairs ($0.001/request)
1056
- const btcCandles = await client.pm("binance/candles/BTCUSDT");
1057
- const ethCandles = await client.pm("binance/candles/ETHUSDT");
1058
- // Also: SOLUSDT, XRPUSDT
1059
- ```
1060
-
1061
- ### Cross-Platform
1062
-
1063
- ```typescript
1064
- // Cross-platform matching pairs ($0.001/request)
1065
- const pairs = await client.pm("matching-markets/pairs");
1066
- ```
1067
-
1068
- All current endpoints are GET. The `pmQuery()` method is available for future POST endpoints.
1069
-
1070
- Works on both `LLMClient` (Base) and `SolanaLLMClient`.
1071
-
1072
- ## Exa Web Search (Powered by Exa)
1073
-
1074
- Access [Exa](https://exa.ai)'s neural web search via x402. No API keys needed — pay-per-request. Available on **`LLMClient` (Base USDC)** and `SolanaLLMClient` (Solana USDC). Use Base as the primary path; the Solana gateway is awaiting `EXA_API_KEY` provisioning.
1075
-
1076
- | Method | Description | Price |
1077
- |---|---|---|
1078
- | `exaSearch(query, options?)` | Neural/keyword web search | $0.01/request |
1079
- | `exaFindSimilar(url, options?)` | Find semantically similar pages | $0.01/request |
1080
- | `exaContents(urls, options?)` | Extract full text from URLs | $0.002/URL |
1081
- | `exaAnswer(query, options?)` | AI answer grounded in web search | $0.01/request |
1082
- | `exa(path, body)` | Generic proxy for any Exa endpoint | varies |
1083
-
1084
- ```typescript
1085
- import { LLMClient } from '@blockrun/llm';
1086
-
1087
- const client = new LLMClient();
1088
-
1089
- // Neural web search ($0.01/request)
1090
- const results = await client.exaSearch("latest AI safety research", { numResults: 5 });
1091
- const news = await client.exaSearch("bitcoin ETF news", { category: "news", numResults: 10 });
1092
-
1093
- // Find similar pages ($0.01/request)
1094
- const similar = await client.exaFindSimilar("https://openai.com/research/gpt-4", { numResults: 5 });
1095
-
1096
- // Extract content from URLs ($0.002/URL)
1097
- const content = await client.exaContents(["https://arxiv.org/abs/2303.08774"]);
1098
-
1099
- // AI-generated answer from live web ($0.01/request)
1100
- const answer = await client.exaAnswer("What is the current state of AI safety research?");
1101
-
1102
- // Generic proxy for any Exa endpoint
1103
- const custom = await client.exa("search", { query: "transformer architecture", numResults: 5 });
1104
- ```
1105
-
1106
- Same surface on `SolanaLLMClient` once Solana-side `EXA_API_KEY` is provisioned.
1107
-
1108
- ## Configuration
1109
-
1110
- ```typescript
1111
- // Default: reads BASE_CHAIN_WALLET_KEY from environment
1112
- const client = new LLMClient();
1113
-
1114
- // Or pass options explicitly
1115
- const client = new LLMClient({
1116
- privateKey: '0x...', // Your wallet key (never sent to server)
1117
- apiUrl: 'https://bridgenode.cc/api', // Optional
1118
- timeout: 60000, // Optional (ms)
1119
- });
1120
- ```
1121
-
1122
- ## Environment Variables
1123
-
1124
- | Variable | Description |
1125
- |----------|-------------|
1126
- | `BASE_CHAIN_WALLET_KEY` | Your Base chain wallet private key (for Base / `LLMClient`) |
1127
- | `SOLANA_WALLET_KEY` | Your Solana wallet secret key - bs58 encoded (for `SolanaLLMClient`) |
1128
- | `BRIDGENODE_API_URL` | API endpoint (optional, default: https://bridgenode.cc/api) |
1129
-
1130
- ## Error Handling
1131
-
1132
- ```typescript
1133
- import { LLMClient, APIError, PaymentError } from '@blockrun/llm';
1134
-
1135
- const client = new LLMClient();
1136
-
1137
- try {
1138
- const response = await client.chat('openai/gpt-4o', 'Hello!');
1139
- } catch (error) {
1140
- if (error instanceof PaymentError) {
1141
- console.error('Payment failed - check USDC balance');
1142
- } else if (error instanceof APIError) {
1143
- console.error(`API error: ${error.message}`);
1144
- }
1145
- }
1146
- ```
1147
-
1148
- ## Testing
1149
-
1150
- ### Running Unit Tests
1151
-
1152
- Unit tests do not require API access or funded wallets:
1153
-
1154
- ```bash
1155
- npm test # Run tests in watch mode
1156
- npm test run # Run tests once
1157
- npm test -- --coverage # Run with coverage report
1158
- ```
1159
-
1160
- ### Running Integration Tests
1161
-
1162
- Integration tests call the production API and require:
1163
- - A funded Base wallet with USDC ($1+ recommended)
1164
- - `BASE_CHAIN_WALLET_KEY` environment variable set
1165
- - Estimated cost: ~$0.05 per test run
1166
-
1167
- ```bash
1168
- export BASE_CHAIN_WALLET_KEY=0x...
1169
- npm test -- test/integration # Run integration tests only
1170
- ```
1171
-
1172
- Integration tests are automatically skipped if `BASE_CHAIN_WALLET_KEY` is not set.
1173
-
1174
- ## Setting Up Your Wallet
1175
-
1176
- ### Base (EVM)
1177
- 1. Create a wallet on Base (Coinbase Wallet, MetaMask, etc.)
1178
- 2. Get USDC on Base for API payments
1179
- 3. Export your private key and set as `BASE_CHAIN_WALLET_KEY`
1180
-
1181
- ```bash
1182
- # .env
1183
- BASE_CHAIN_WALLET_KEY=0x...
1184
- ```
1185
-
1186
- ### Solana
1187
- 1. Create a Solana wallet (Phantom, Backpack, Solflare, etc.)
1188
- 2. Get USDC on Solana for API payments
1189
- 3. Export your secret key and set as `SOLANA_WALLET_KEY`
1190
-
1191
- ```bash
1192
- # .env
1193
- SOLANA_WALLET_KEY=...your_bs58_secret_key
1194
- ```
1195
-
1196
- Note: Solana transactions are gasless for the user - the CDP facilitator pays for transaction fees.
1197
-
1198
- ## Security
1199
-
1200
- ### Private Key Safety
1201
-
1202
- - **Private key stays local**: Your key is only used for signing on your machine
1203
- - **No custody**: BlockRun never holds your funds
1204
- - **Verify transactions**: All payments are on-chain and verifiable
1205
-
1206
- ### Best Practices
1207
-
1208
- **Private Key Management:**
1209
- - Use environment variables, never hard-code keys
1210
- - Use dedicated wallets for API payments (separate from main holdings)
1211
- - Set spending limits by only funding payment wallets with small amounts
1212
- - Never commit `.env` files to version control
1213
- - Rotate keys periodically
1214
-
1215
- **Input Validation:**
1216
- The SDK validates all inputs before API requests:
1217
- - Private keys (format, length, valid hex)
1218
- - API URLs (HTTPS required for production, HTTP allowed for localhost)
1219
- - Model names and parameters (ranges for max\_tokens, temperature, top\_p)
1220
-
1221
- **Error Sanitization:**
1222
- API errors are automatically sanitized to prevent sensitive information leaks.
1223
-
1224
- **Monitoring:**
1225
- ```typescript
1226
- const address = client.getWalletAddress();
1227
- console.log(`View transactions: https://basescan.org/address/${address}`);
1228
- ```
1229
-
1230
- **Keep Updated:**
1231
- ```bash
1232
- npm update @blockrun/llm # Get security patches
1233
- ```
1234
-
1235
- ## TypeScript Support
1236
-
1237
- Full TypeScript support with exported types:
1238
-
1239
- ```typescript
1240
- import {
1241
- LLMClient,
1242
- OpenAI,
1243
- type ChatMessage,
1244
- type ChatResponse,
1245
- type ChatOptions,
1246
- type ChatCompletionOptions,
1247
- type Model,
1248
- // Smart routing types
1249
- type SmartChatOptions,
1250
- type SmartChatResponse,
1251
- type RoutingDecision,
1252
- type RoutingProfile,
1253
- type RoutingTier,
1254
- APIError,
1255
- PaymentError,
1256
- } from '@blockrun/llm';
1257
-
1258
- // chatCompletionStream returns a standard fetch Response with SSE body
1259
- const streamResponse: Response = await client.chatCompletionStream(model, messages, options);
1260
-
1261
- // OpenAI-compat stream returns AsyncIterable
1262
- const stream: AsyncIterable<OpenAIChatCompletionChunk> = await openaiClient.chat.completions.create({
1263
- model, messages, stream: true
1264
- });
1265
- ```
1266
-
1267
- ## Agent Wallet Setup
1268
-
1269
- One-line setup for agent runtimes (Claude Code skills, MCP servers, etc.):
1270
-
1271
- ```typescript
1272
- import { setupAgentWallet } from '@blockrun/llm';
1273
-
1274
- // Auto-creates wallet if none exists, returns ready client
1275
- const client = setupAgentWallet();
1276
- const response = await client.chat('openai/gpt-5.4', 'Hello!');
1277
- ```
1278
-
1279
- For Solana:
1280
-
1281
- ```typescript
1282
- import { setupAgentSolanaWallet } from '@blockrun/llm';
1283
-
1284
- const client = await setupAgentSolanaWallet();
1285
- const response = await client.chat('anthropic/claude-sonnet-4.6', 'Hello!');
1286
- ```
1287
-
1288
- Check wallet status:
1289
-
1290
- ```typescript
1291
- import { status } from '@blockrun/llm';
1292
-
1293
- await status();
1294
- // Wallet: 0xCC8c...5EF8
1295
- // Balance: $5.30 USDC
1296
- ```
1297
-
1298
- ## Wallet Discovery and Migration
1299
-
1300
- The SDK can discover compatible wallets for an explicit, user-confirmed
1301
- migration. It never automatically makes a discovered provider wallet active:
1302
-
1303
- ```typescript
1304
- import { scanWallets, scanSolanaWallets } from '@blockrun/llm';
1305
-
1306
- // Scans ~/.<dir>/wallet.json for Base wallets
1307
- const baseWallets = scanWallets();
1308
-
1309
- // Scans ~/.<dir>/solana-wallet.json and ~/.brcc/wallet.json
1310
- const solWallets = scanSolanaWallets();
1311
- ```
1312
-
1313
- `getOrCreateWallet()` always uses `~/.blockrun/.session` (or an explicit
1314
- wallet environment variable, or the legacy `~/.blockrun/wallet.key`). Review
1315
- the discovered addresses and import one explicitly if you intend to switch
1316
- wallets.
1317
-
1318
- ### Upgrading from a provider wallet
1319
-
1320
- Earlier versions adopted the most recently written provider wallet
1321
- automatically. If you relied on that, the first run after upgrading creates a
1322
- fresh BlockRun wallet and prints the addresses it found, so you can import the
1323
- one you actually own:
1324
-
1325
- ```
1326
- NOTICE: BlockRun created a new wallet, but also found existing wallet(s)
1327
- belonging to other applications on this system:
1328
-
1329
- 0x88f9B82462f6C4bf4a0Fb15e5c3971559a316e7f
1330
- ...
1331
- ```
1332
-
1333
- Adopt one deliberately:
1334
-
1335
- ```typescript
1336
- import { listDiscoveredWallets, importWallet } from '@blockrun/llm';
1337
-
1338
- for (const w of listDiscoveredWallets()) {
1339
- console.log(w.address, 'from', w.source);
66
+ })) {
67
+ process.stdout.write(chunk.choices?.[0]?.delta?.content ?? "");
1340
68
  }
1341
-
1342
- importWallet('0x88f9B82462f6C4bf4a0Fb15e5c3971559a316e7f');
1343
69
  ```
1344
70
 
1345
- `importWallet()` writes your current wallet to
1346
- `~/.blockrun/.session.backup-<timestamp>` before switching, so adopting a wallet
1347
- never strands funds in the old one. Solana: `listDiscoveredSolanaWallets()` and
1348
- `importSolanaWallet()`.
1349
-
1350
- Addresses shown are derived from the discovered key itself, and `importWallet()`
1351
- matches on that derived address — so a wallet file cannot claim an address it
1352
- cannot sign for, nor be adopted by one. `listDiscoveredWallets()` never returns
1353
- private keys.
1354
-
1355
- For a single run without changing anything, use
1356
- `export BRIDGENODE_WALLET_KEY=<private-key>`.
1357
-
1358
- ## Response Caching
1359
-
1360
- The SDK caches responses to avoid duplicate payments:
1361
-
1362
- ```typescript
1363
- import { getCachedByRequest, saveToCache, clearCache } from '@blockrun/llm';
1364
-
1365
- // Automatic TTLs by endpoint:
1366
- // - Search: 15 minutes
1367
- // - Models: 24 hours
1368
- // - Chat/Image: no cache (every call is unique)
1369
-
1370
- // Manual cache management
1371
- clearCache(); // Remove all cached responses
1372
- ```
1373
-
1374
- ## Cost Logging
1375
-
1376
- Track spending across sessions:
1377
-
1378
- ```typescript
1379
- import { logCost, getCostSummary } from '@blockrun/llm';
1380
-
1381
- // Costs are logged to ~/.blockrun/data/costs.jsonl
1382
- const summary = getCostSummary();
1383
- console.log(`Total: $${summary.totalUsd.toFixed(2)}`);
1384
- console.log(`Calls: ${summary.calls}`);
1385
- console.log(`By model:`, summary.byModel);
1386
- ```
1387
-
1388
- ## Anthropic SDK Compatibility
1389
-
1390
- Use the official Anthropic SDK interface with BlockRun's pay-per-request backend:
1391
-
1392
- ```typescript
1393
- import { AnthropicClient } from '@blockrun/llm';
1394
-
1395
- const client = new AnthropicClient(); // Auto-detects wallet, auto-pays
1396
-
1397
- const response = await client.messages.create({
1398
- model: 'claude-sonnet-4-6',
1399
- max_tokens: 1024,
1400
- messages: [{ role: 'user', content: 'Hello!' }],
1401
- });
1402
- console.log(response.content[0].text);
1403
-
1404
- // Any model works in Anthropic format
1405
- const gptResponse = await client.messages.create({
1406
- model: 'openai/gpt-5.4',
1407
- max_tokens: 1024,
1408
- messages: [{ role: 'user', content: 'Hello from GPT!' }],
1409
- });
1410
- ```
1411
-
1412
- The `AnthropicClient` wraps the official `@anthropic-ai/sdk` with a custom fetch that handles x402 payment automatically. Your private key never leaves your machine. The Mythos-class `claude-fable-5` alias is available here too (1M context, always-on thinking).
1413
-
1414
- ## Links
1415
-
1416
- - [Website](https://bridgenode.cc)
1417
- - [Documentation](https://github.com/bridgenode/awesome-blockrun/tree/main/docs)
1418
- - [GitHub](https://github.com/blockrunai/bridgenode-llm-ts)
1419
- - [Telegram](https://t.me/+mroQv4-4hGgzOGUx)
1420
-
1421
- ## Frequently Asked Questions
1422
-
1423
- ### What is @blockrun/llm?
1424
- @blockrun/llm is a TypeScript SDK that provides pay-per-request access to 40+ large language models from OpenAI, Anthropic, Google, xAI, DeepSeek, Moonshot, and more. It uses the x402 protocol for automatic USDC micropayments — no API keys, no subscriptions, no vendor lock-in.
1425
-
1426
- ### How does payment work?
1427
- When you make an API call, the SDK automatically handles x402 payment. It signs a USDC transaction locally using your wallet private key (which never leaves your machine), and includes the payment proof in the request header. Settlement is non-custodial and instant on Base or Solana.
1428
-
1429
- ### What is smart routing / ClawRouter?
1430
- ClawRouter is a built-in smart routing engine that analyzes your request across <!-- br:clawrouter.dimensions -->15<!-- /br:clawrouter.dimensions --> dimensions and automatically picks the cheapest model capable of handling it. Routing happens locally in under 1ms. It can save up to <!-- br:savings.autoVsBaselinePct -->87<!-- /br:savings.autoVsBaselinePct -->% on LLM costs compared to using premium models for every request.
71
+ ---
1431
72
 
1432
- ### Does it support streaming?
1433
- Yes — as of v1.6.1. Use `client.chatCompletionStream()` for native streaming or `stream: true` in the OpenAI-compatible client. Payment is handled automatically: the SDK signs USDC payment before streaming begins, and caches payment requirements per model so subsequent calls skip the 402 round-trip (~200ms faster).
73
+ ## Supported Chain
1434
74
 
1435
- ### How much does it cost?
1436
- Pay only for what you use. Prices start at $0.0002 per request (GPT-5 Nano). There are no minimums, subscriptions, or monthly fees. $5 in USDC gets you thousands of requests.
75
+ | Chain | Payment | Status |
76
+ |-------|---------|--------|
77
+ | **Solana Mainnet** | USDC (SPL) | ✅ Active |
1437
78
 
1438
- ### Does it support both Base and Solana?
1439
- Yes. Use `LLMClient` for Base (EVM) payments and `SolanaLLMClient` for Solana payments. Same API, different payment chain.
79
+ ---
1440
80
 
1441
81
  ## License
1442
82