Core Fundamentals ⏱ 9 min read 📁 examples/21_observability.mjs

Lesson 28: Universal Event Telemetry Surface

Inspect the SDK universal event telemetry surface across all event families with hierarchical runId and parentRunId correlation.

1. Why This Feature Exists

Complex multi-agent architectures generate hundreds of asynchronous events. Merci SDK assigns every execution context a unique runId and links child executions via parentRunId for seamless APM tracing.

2. Key Symbols and Exports

runId & parentRunId
Correlates parent agent workflows with nested subagents and tool calls in distributed traces.
Event Families
api:*, tool:*, session:*, workflow:*, discussion:*, state_machine:*, memory:*
wireClient(client)
Standardized telemetry formatting utility used across all examples.

3. Implementation Walkthrough

import {
    ProviderType,
    resolveProvider,
    initializeClient,
    loadCredentials,
    getDefaultModel
} from 'merci-sdk';

const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });

// Subscribe to all event families
const eventNames = [
    'api_request', 'api_response', 'api_retry',
    'tool_start', 'tool_finish',
    'session:start', 'session:end',
    'workflow:step', 'memory:distill'
];

eventNames.forEach(evt => {
    client.on(evt, (data) => {
        console.log(`[${evt}] (runId: ${data?.runId ?? 'root'})`, data);
    });
});

const session = client.session(getDefaultModel({ provider: PROVIDER }));
await session.run('Analyze the top 3 considerations for distributed event streaming.');

4. Wire and Telemetry Mechanics

All core classes emit events onto the central client bus. Event payloads include timestamps, run IDs, token counts, and execution metrics.

5. Run the Example

Execute the runnable example directly from the repository root:

node examples/21_observability.mjs

Read the full example: examples/21_observability.mjs →

Expected Output: Runs a sample prompt while logging the complete lifecycle event sequence with correlated IDs.