Guidance for AI coding agents working with the BlockRun TypeScript SDK.
@blockrun/llm is a TypeScript SDK that cuts LLM costs by up to 84%: its bundled smart router (Router Core V3) picks the cheapest capable model for every request — locally, in <1ms — and pays per-request in USDC via x402 on Base or Solana. API key account billing or x402 wallet payments. Use smartChat() for routed (cheapest) calls; chat() to pin a model. Includes 9 fully-free models — Nemotron 3.5 Lightning and Nemotron 3 Ultra 550B (1M ctx), Nemotron 3 Nano Omni (multimodal, 256K), Nemotron 3 Nano 30B, Llama 3.2 11B Vision, Cohere North Mini Code (256K coding) and Poolside Laguna XS 2.1. The free tier is no longer NVIDIA-only, so pin them by full model id rather than by an nvidia/* prefix, or use routingProfile: 'eco', which ranks the free tier first. (There is no 'free' routing profile — routingProfile accepts 'eco' | 'auto' | 'premium'.)
Package: @blockrun/llm (npm)
Node: >=20
Networks: Solana (recommended) and Base (EVM)
Payment: USDC via x402 v2 (or $0 on the free tier)
blockrun-llm-ts/
├── src/
│ ├── index.ts # Package exports
│ ├── client.ts # LLMClient class
│ ├── image.ts # Image generation
│ ├── openai-compat.ts # OpenAI-compatible client wrapper
│ ├── types.ts # TypeScript interfaces
│ ├── validation.ts # Input validation
│ ├── wallet.ts # Wallet operations (Base + Solana)
│ └── x402.ts # x402 payment protocol
├── test/ # Vitest tests
├── dist/ # Build output (generated)
├── package.json
├── tsconfig.json
└── README.md
# Install
pnpm install
# Build
pnpm build # Build with tsup (CJS + ESM + types)
pnpm dev # Build in watch mode
# Testing
pnpm test # Run tests in watch mode
pnpm test run # Run tests once
pnpm test -- --coverage # With coverage
# Code Quality
pnpm lint # ESLint
pnpm typecheck # TypeScript check- Strict mode enabled
- Export types explicitly from
index.ts - Use
interfacefor objects,typefor unions - Full JSDoc comments for public APIs
- tsup bundler (CJS, ESM, and .d.ts output)
- Tree-shakeable exports
- Solana dependencies are optional
LLMClient- Main client classchat()- Simple chat methodchatCompletion()- Full OpenAI-compatible responsesmartChat()/smartChatCompletion()- bundled Router Core V3 (see below)- Automatic x402 payment handling
client.smartChat(prompt, { routingProfile? })picks the cheapest capable model per request. Profiles:'eco' | 'auto' | 'premium'(defaultauto).smartChatCompletion(messages, options)routes full agent/tool turns;route(prompt)inspects a decision without paying. Theblockrun/auto|eco|premiumaliases work inchat(),chatCompletion(), andchatCompletionStream()on both chain clients and the OpenAI-compat layer — NOT in the Anthropic-compat layer (it proxies/v1/messagesraw).- Router Core is bundled; consumers install no separate router package.
- The default strategy is the deterministic portfolio router
(
routing.method: 'portfolio'): local classification, hard capability filters, rankedrouting.candidates. Candidate policy: the ranking is trusted as-is (including ids withheld from/v1/models); thefree/*proxy namespace is mapped to catalog-listednvidia/*ids (dropped when proxy-only). The SDK buildsrouting.fallbacksfrom that ranking andchat()walks it automatically on transient errors (timeout / network / 429 / 5xx) raised before any payment was sent (retryDisposition(err) === "unpaid"); a failure after a payment never buys another model. Caller-suppliedfallbackModelswins over the routed chain. - Routing types (
RoutingDecision,RoutingTier,RoutingTaskType,RoutingTierConfig) are derived from@blockrun/router-core— a devDependency pinned to the reviewed Router Core commit — and ship inlined in the SDK's.d.ts, so consumers typecheck without installing another package. Do not hand-edit these shapes; re-pin the router-core commit when upgrading the engine (procedure in CONTRIBUTING.md).
| File | Purpose |
|---|---|
client.ts |
Main LLMClient with chat(), chatCompletion(), smartChat(), smartChatCompletion(), route(), listModels() |
router-adapter.ts |
Bundled Router Core V3 adapter — candidate mapping (free/*→nvidia/*), capacity filter, shared transient-error logic |
solana-client.ts |
SolanaLLMClient with the same routing/fallback surface, paid on Solana |
tsup.config.ts |
Build config; dts.resolve inlines @blockrun/router-core types into the shipped .d.ts |
x402.ts |
x402 payment protocol implementation |
wallet.ts |
Base wallet: resolution/discovery/adoption delegated to @blockrun/core (bundled into dist); funding/messaging surface SDK-local |
solana-wallet.ts |
Solana wallet — deliberately SDK-local (core has no Solana key store yet); does not honor BLOCKRUN_HOME |
validation.ts |
Input validation for keys, URLs, parameters |
types.ts |
TypeScript interfaces for API |
openai-compat.ts |
OpenAI SDK compatible wrapper |
- Uses
viemfor signing - Environment:
BASE_CHAIN_WALLET_KEY
- Uses
@solana/web3.js(optional dependency) - Environment:
SOLANA_WALLET_KEY(base58-encoded secret key) - Gasless transactions (facilitator pays fees)
pnpm test runRequires funded wallet:
export BASE_CHAIN_WALLET_KEY=0x...
pnpm test -- test/integrationpnpm build
npm publish --access public- Private keys never leave the machine
- HTTPS required for production
- Solana keys are base58 encoded
- Error messages are sanitized
All service clients accept apiKey / BLOCKRUN_API_KEY. Shared transport in
src/api-key.ts authenticates against https://api.blockrun.ai, prevents x402
replay on account errors, and scopes credentials to the configured origin.
setupAgentClient() chooses account mode first, then preserves existing chain
preferences/Base-only wallets, otherwise initializes Solana. The named wallet
setup functions remain chain-specific. Register at https://user.blockrun.ai.