DOCS / TYPESCRIPT SDK Docs overview

TypeScript SDK

Control worlds from code.

The SDK configures definitions, creates instances and runs experiments. Agents operate through the native interfaces issued by a world.

Client

import { Counterworld } from "@counterworld/sdk";

const counterworld = new Counterworld({
  apiKey: process.env.COUNTERWORLD_API_KEY,
  // baseUrl: "https://api.counterworld.dev",
});
OptionTypeRequiredDescription
apiKeystringYesServer-side project API key.
baseUrlstringNoControl API endpoint; defaults to Counterworld Cloud.
timeoutnumberNoRequest timeout in milliseconds.

Definitions

Definitions are reusable, versioned descriptions of systems, users, history and evaluators.

MethodReturnsPurpose
definitions.create(input)DefinitionDraftCreate a draft from catalog or authored components.
definitions.fromOpenAPI(input)DefinitionDraftInfer a draft from one or more service specifications.
definitions.get(id)DefinitionRetrieve a definition and its published versions.
draft.validate()ValidationReportCheck schemas, mutations, relations and evaluator references.
draft.publish()DefinitionVersionFreeze a validated version for reproducible worlds.
const draft = await counterworld.definitions.fromOpenAPI({
  name: "fulfillment-ops",
  specs: ["./orders.yaml", "./warehouse.yaml"],
});

const validation = await draft.validate();
if (!validation.valid) throw new Error(validation.summary);

const version = await draft.publish();

Worlds

A world is one isolated, populated and running instance of a published definition.

MethodReturnsPurpose
worlds.create(input)WorldCreate an instance from a definition or inline definition input.
worlds.get(id)WorldReconnect to an existing world.
world.pause()voidStop simulated time and actor execution.
world.resume()voidResume actor and scheduled-event execution.
world.destroy()voidDelete the instance and revoke issued credentials.
const world = await counterworld.worlds.create({
  definition: version.id,
  seed: "fulfillment-regression-17",
  clock: { startAt: "2026-04-01T09:00:00Z" },
});

Agent interfaces

Issue scoped, revocable credentials for one production-shaped interface.

const access = await world.interfaces.connect("shopify-admin", {
  scopes: ["read_orders", "write_refunds"],
  expiresIn: "2h",
});

// Pass only these values to the tested agent.
console.log(access.baseUrl);
console.log(access.accessToken);
MethodReturnsPurpose
interfaces.list()Interface[]List APIs, SDK adapters and MCP surfaces available in the world.
interfaces.connect(name, options)InterfaceAccessIssue an endpoint and scoped credentials.
interfaces.revoke(id)voidRevoke previously issued credentials.

Time, checkpoints and forks

await world.clock.advance({ days: 7, mode: "until-idle" });

const checkpoint = await world.checkpoints.create({
  name: "after-week-one",
});

const branch = await world.forks.create({
  from: checkpoint.id,
  name: "challenger-agent",
});

const diff = await world.inspect.diff({
  from: checkpoint.id,
  to: branch.id,
});
MethodReturnsPurpose
clock.advance(input)AdvanceResultRelease scheduled events and actor decisions through a target time.
checkpoints.create(input)CheckpointCapture complete state, clock, actors and pending events.
checkpoints.restore(id)WorldRestore the current world to an earlier checkpoint.
forks.create(input)WorldCreate an independent world with the checkpoint’s exact lineage.
inspect.diff(input)StateDiffCompare entities, events, schedules, metrics and violations.

Simulation

await world.simulation.configure({
  population: {
    buyers: { discountSensitivity: 0.72, returnPropensity: 0.16 },
    storeAdmins: { approvalDelayHours: 6 },
    warehouseOperators: { shiftCoverage: 0.82 },
  },
});

await world.simulation.schedule({
  at: "2026-04-15T00:00:00Z",
  change: { seasonalDemand: 1.35 },
});
MethodReturnsPurpose
simulation.configure(input)SimulationConfigUpdate buyer, admin, employee or operator behavior and event policies.
simulation.schedule(input)ScheduledChangeApply a configuration change at simulated time.
simulation.intervene(input)InterventionApply an explicit user, traffic or system change immediately.
simulation.status()SimulationStatusInspect actor populations, event queue and current configuration.

Evaluation

const report = await world.evaluate({
  metrics: ["gross_margin", "repeat_purchase_rate"],
  invariants: ["no_negative_inventory"],
  compareTo: checkpoint.id,
});

console.log(report.passed);
console.log(report.metrics);
console.log(report.violations);
MethodReturnsPurpose
world.evaluate(input)EvaluationReportRun metrics, predicates, delayed checks and invariants.
inspect.state(query)StateResultRead privileged state outside the agent boundary.
inspect.events(query)EventPageRead agent, user and system events.
report.export(format)RunArtifactExport JSON, JSONL or OpenTelemetry-compatible run data.

Errors

import { CounterworldError } from "@counterworld/sdk";

try {
  await world.clock.advance({ days: 30 });
} catch (error) {
  if (error instanceof CounterworldError) {
    console.error(error.code);       // WORLD_PAUSED
    console.error(error.requestId);  // req_...
  }
}

SDK errors include a stable code, human-readable message, request identifier and retryability flag.