OpenTelemetry instrumentation for multi-agent LLM systems. One createSwarm() call gives you GenAI-semconv spans for every agent and every model call, fallback promotions recorded as span events, critic catches as span events, and a mirrored local event bus so a live UI can stream exactly what your tracing backend stores (one pipeline, two consumers, and they can never disagree).
Built during the Agents of SigNoz hackathon to instrument DevSwarm, extracted because any multi-agent system has the same observability problem: parallel agents, model fallbacks and review loops are opaque without traces, and nobody wants to hand-wire OTel for every role.
npm install github:himanshu748/otel-swarmgit clone https://github.com/himanshu748/otel-swarm && cd otel-swarm
npm install
npm run example # spans to console
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 npm run example # spans to SigNozThe example runs a fake four-agent pipeline: a task root span, parallel agent spans, an LLM call whose primary fails and promotes to its fallback (visible as a fallback_promotion span event), and a critic catch. Open SigNoz and the whole story is one trace.
| Option / env var | Default | Meaning |
|---|---|---|
service (required) |
none | service.name on every span |
version |
0.0.0 |
service.version |
otlpEndpoint / OTEL_EXPORTER_OTLP_ENDPOINT |
unset | OTLP/HTTP base URL; /v1/traces is appended (SigNoz: http://localhost:4318) |
tracesEndpoint / OTEL_EXPORTER_OTLP_TRACES_ENDPOINT |
unset | Full traces URL, used as-is; wins over the base URL |
exporter |
none | Any OTel SpanExporter (e.g. InMemorySpanExporter in tests); wins over both endpoints |
With no endpoint and no exporter, spans go to the console. Requires Node ^18.19 || >=20.6 (OpenTelemetry JS 2.x).
import { createSwarm } from 'otel-swarm';
const swarm = createSwarm({ service: 'my-swarm', otlpEndpoint: 'http://localhost:4318' });
await swarm.task('generation', { 'my.prompt': prompt }, async (root) => {
const plan = await swarm.agent('planner', () =>
swarm.llm('planner', {
model: 'primary-model-id',
fallbackModel: 'fallback-model-id',
call: async (model) => {
const res = await yourProviderCall(model);
return { content: res.text, inputTokens: res.usage.in, outputTokens: res.usage.out };
}
})
);
await swarm.agent('critic', async (span) => {
swarm.reviewEvents(span, issues); // each issue becomes a critic_catch span event
});
});
swarm.events.on('event', (e) => { /* stream llm_start/llm_end/fallback/critic_catch to your UI */ });task(name, attrs, fn): root span per end-to-end run.agent(role, fn): child span per agent unit of work.llm(role, {model, fallbackModel, call}): GenAI semconv span per model call;call(model)is invoked again withfallbackModelif the primary throws, and the switch is a span event, so "why did the model change" is answerable from the trace.gen_ai.request.modelstays the model you attempted;gen_ai.response.modelis the one that answered.reviewEvents(span, issues): review findings as span events.events: an EventEmitter mirroring every span lifecycle, withtraceIdon LLM events for deep-linking your UI to the exact trace.
dashboards/: three importable dashboards (POST each to/api/v1/dashboards): Generation Overview, LLM Economics (tokens and latency per role and model, fallback pressure), Review-Gate Funnel.alerts/: two alert rules in v2alpha1 schema (POST to/api/v2/rules): fallback-promotion spike, and review catch-rate flatline (if your reviewer suddenly catches nothing, the reviewer broke, not the code).casting.yamlandcasting.yaml.lock: the Foundry config the SigNoz behind this library was installed from, so the backend the dashboards target is reproducible rather than assumed.
Every dashboard carries a service variable, so you set your service name once in the SigNoz UI instead of editing four queries. It defaults to otel-swarm-demo, which is what npm run example reports as, so the pack has data the moment you import it.
The queries read what this library already emits: span names llm.* and agent.*, a root generation span, and swarm.role on every span. Three attributes on your root span are yours to set, and the Generation Overview and Review-Gate Funnel dashboards read them:
root.setAttributes({
'swarm.generation.verdict': 'pass', // or 'fail'
'swarm.generation.critic_catches': 1,
'swarm.generation.regenerations': 0
});example/demo.js sets them, so you can see the shape.
npm ci
npm test # node:test suite with an in-memory exporter, no backend needed
npm run exampleMIT, see LICENSE.