DIM · Dagon Inference Marketplace

The SDK

@im/sdk is DIM's TypeScript client for buyers. It verifies discovery answers against keys you pin, manages channels in a wallet backed by your Solana vault, and gives you an OpenAI-style chat.completions.create that signs a hold for every request, checks every receipt and pays by voucher. It runs under Bun, or under Node 22+ as an ESM bundle, and has no account or API key of its own.

Install

The SDK is a workspace package in the DIM repository, not an npm release. From a checkout:

bun install
bun run --cwd packages/sdk build      # optional: Node ESM bundle in packages/sdk/dist
bun test packages/sdk                 # the SDK's own tests

It has three entry points. @im/sdk/solana is separate so in-memory workloads never load Solana libraries.

Import Contains
@im/sdk DiscoveryClient, ChannelWallet, MemoryChannelBackend, MarketplaceClient, AdaptivePlanner, verifyReceipt, FileWalletStore, MarketplaceError, marketplaceDomain and their types
@im/sdk/solana SolanaChannelBackend, SolanaChannelInstructions
@im/sdk/release-feed SettlementReleaseFeed, for choosing a program release for new funds

The four pieces

DiscoveryClient

Reads routes from a discovery index, or directly from one seller, and returns only routes it could verify.

Option Default Meaning
indexer none { url, key } or { url, keys: [old, new] } to pin the index's attestation key, up to 16 keys.
operators none Probe operator keys to trust. Other observations are ignored.
capacityMaxAgeSeconds 30 How long a capacity report counts as fresh.
observationMaxAgeSeconds 600 How long after its window a probe observation counts.
timeoutMs 5,000 Per HTTP request.
pageSize 100 Routes per page, up to 200.

Methods: list({ modelIdentityId?, family? }) follows every page; get(routeId) reads one route; direct({ url, key }) reads a seller's own listing and capacity.

ChannelWallet

Opens, tops up and closes channels, creates and revokes Mandates, and keeps local accounting so a request is never signed for more than a channel can pay. It needs a backend: SolanaChannelBackend for real money, or MemoryChannelBackend for simulations and tests. Pass mandates: [leaf, parent, …] to act as an agent under a Mandate.

Methods include open, topUp, channels, beginClose, closeOnRevoked, finalize, createMandate, revokeMandate, refreshMandate, closeMandate, mandates and closeStore.

MarketplaceClient

The chat client. One client owns all agent lanes and one cumulative voucher stream per channel.

Option Default Meaning
wallet, domain required Your wallet, and the program domain it signs for (backend.domain on Solana).
models none Maps OpenAI model names to exact model identity IDs.
policy none { zdr?, region? } every route must meet.
defaultCapMicros route's w_req_micros Cap for requests that do not set one.
maxConcurrent 1,024 Requests in flight across all lanes.
batchSize, batchDelayMs 32, 10 Receipts per voucher, and how long to gather a batch.
pendingPerChannel 256 Unpaid attempts and receipts per channel before new requests are refused.
settlementTimeoutMs 5,000 Timeout for receipt lookups and voucher posts.
planner AdaptivePlanner Your own route selector, if you want one.

Per call, create(body, options) accepts capMicros, modelIdentityId or modelIdentityIds, routeId, zdr, region, signal, timeoutMs (default 120,000) and maxAttempts (default 3, up to 8).

The planner

AdaptivePlanner picks a route for each request from the routes you installed with setRoutes or refresh. See choosing a route.

A simulation without Solana

MemoryChannelBackend keeps channels in memory with a starting free balance, which is handy for tests against a local gateway. The gateway must see the same channels, so this only works with a simulated seller that shares the in-memory view.

import { ChannelWallet, MarketplaceClient, MemoryChannelBackend, marketplaceDomain } from "@im/sdk";

const wallet = new ChannelWallet({ backend: new MemoryChannelBackend(1_000_000n) }); // 1 USDC of play money
const sdk = new MarketplaceClient({
  wallet,
  domain: marketplaceDomain(), // off-chain simulation only: zero genesis hash
  models: { "sim-llm": "0".repeat(64) },
});
console.log(sdk.pendingReceipts().length); // 0

What the SDK guarantees, and what it does not

  • It never signs a hold the wallet or Mandate cannot cover, and never pays a receipt that fails verification.
  • It never reroutes a request after a 2xx status, because that attempt may already be billable.
  • It hashes the exact request bytes with a fresh salt, sends them only to the chosen seller, and rejects redirects.
  • It does not make an unverifiable seller honest: claims like zero data retention stay claims.

The SDK's contract-to-test map is in packages/sdk/CONFORMANCE.md.