DIM · Dagon Inference Marketplace

Budgets for agent swarms

To run a swarm of agents on DIM, give every concurrent worker its own lane on one shared client, size each seller's channel for concurrency times cap plus a small voucher backlog, and give sub-agents their own child Mandates that are narrower than the parent's. The program enforces every limit on-chain, and revoking the top Mandate stops every agent beneath it.

The code on this page assumes the sdk, wallet and route from the buyer quickstart.

One client, many lanes

A MarketplaceClient owns all lanes and one cumulative voucher stream per channel. Give each worker its own lane:

const workers = Array.from({ length: 50 }, (_, i) => sdk.agent(`worker-${i}`));
const results = await Promise.all(workers.map((agent, i) =>
  agent.chat.completions.create(
    { model: "my-model", messages: [{ role: "user", content: `Summarize item ${i} in one line.` }] },
    { capMicros: 20_000n },
  )));
console.log(results.length, "replies");
  • sdk.agent(id) returns the same lane for the same id. Lanes are numbered 1 to 65,535; sdk.chat uses lane 0.
  • A lane admits one request at a time until that request's status line arrives; a second concurrent call on the same lane fails with lane_busy. Streaming on a lane does not block the next request on it once the response has started.
  • Every hold carries its lane and a strictly increasing per-lane counter, so the seller can reject replays (lane_replay).
  • maxConcurrent (default 1,024) caps requests in flight across all lanes.
  • All lanes share one channel per seller. Use one client per wallet; two clients on the same channel would fight over lane counters and voucher sequence.

Sizing channels for concurrency

Every request in flight reserves its full cap on the channel until its receipt arrives, and every verified charge stays counted until the channel closes. The SDK only signs a hold when

charges so far + caps in flight + new cap ≤ channel budget

and the seller only admits it when the cap fits in budget − redeemed − unvouchered charges − caps in flight.

Example: 50 workers spread over 5 sellers is about 10 requests in flight per seller. With a 20,000 micro-USDC cap, that is 200,000 of exposure per channel. If each worker makes 100 requests averaging 800 micro-USDC, each channel will absorb about 800,000 in charges over the run. A 1,500,000 (1.5 USDC) channel per seller leaves room for both, and 5 such channels commit 7.5 USDC against the Mandate's period budget.

When a channel runs low, wallet.topUp(route, terms) opens a successor channel for that seller. Close the old one when it goes idle.

A Mandate tree for sub-agents

Keep the owner key out of the swarm. The owner writes one Mandate for an orchestrator key; the orchestrator writes narrower child Mandates for sub-agent keys. Each child's budget must fit in what the parent has left this period, and its cap, expiry, grace and sellers can only shrink. Chains go at most three ancestors deep.

import type { Mandate } from "@im/sdk";

declare const parent: Mandate;      // the orchestrator's Mandate, read with backend.readMandate
declare const subAgentKey: string;  // base58 public key of the sub-agent
// `wallet` here is the orchestrator's wallet: its backend signs with the orchestrator key,
// constructed with `mandates: [parent]`.

const child = await wallet.createMandate({
  agentKey: subAgentKey,
  nonce: 1n,
  parent: parent.address,
  budgetPerPeriod: parent.budgetPerPeriod / 10n, // a tenth of the parent's period budget
  periodSeconds: parent.periodSeconds,
  perRequestCap: parent.perRequestCap,
  maxGraceSeconds: parent.maxGraceSeconds,
  sellers: parent.sellers.slice(0, 2),           // a subset of the parent's sellers
  expiry: parent.expiry,
});
console.log("child Mandate:", child.address);

The sub-agent then runs its own wallet with mandates: [child, parent], leaf first, and opens its own channels. Opening a channel under the child counts against the child's period budget and every ancestor's, so the parent's budget is a hard ceiling for the whole subtree.

Stopping a swarm

  • Everything: revoke the top Mandate. Every descendant is dead at once, no agent can open a channel, and anyone can call close_on_revoked on their channels.
  • One sub-agent: revoke its Mandate. The orchestrator, as the agent of an ancestor, may do this.
  • Spend already in flight: requests already admitted finish; their caps bound the cost. Unspent channel budgets return to the vault after the grace period.

Evidence from the repository

The swarm demo in demo/swarm runs its b-swarm case with workers paying five independent simulated sellers under one bounded Mandate; docs/mvp/demo-results.md records its measured results and limitations.