Core Fundamentals ⏱ 6 min read 📁 examples/01_quickstart.mjs

Lesson 01: Quickstart: Client Initialization and Streaming

Learn the fundamental lifecycle of the Merci SDK: resolving provider configurations, initializing the client, configuring session-level system directives, and consuming streamed completions with real-time quota accounting.

1. Why This Feature Exists

Most AI SDKs couple the developer to a single proprietary provider or enforce opaque server-side chat threads. Merci SDK standardizes provider communication through thin adapters while keeping conversation history stateless and developer-owned. In Lesson 1, you see the bare essentials: a minimal-dependency core, explicit model selection, and generator-based streaming.

2. Key Symbols and Exports

resolveProvider()
Inspects env vars (PROVIDER) and defaults gracefully to the configured provider.
initializeClient({ provider, options })
Instantiates the provider-selected HTTP or SDK transport client.
client.session(model)
Creates a fluent ChatSessionBuilder for chainable configuration.
session.step({ messages: [message] })
Async generator that yields streamed text chunks, thought signatures, and quota metrics.
finalState.messages
The immutable, updated history returned when the generator completes.

3. Implementation Walkthrough

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

// 1. Resolve provider & model tier
const SELECTED_PROVIDER = resolveProvider();
const MODEL = getDefaultModel({ provider: SELECTED_PROVIDER });

// 2. Initialize client with credentials
const { client } = await initializeClient({ provider: SELECTED_PROVIDER, options: loadCredentials({ provider: SELECTED_PROVIDER }) });

// 3. Build session
const session = client.session(MODEL)
    .withSystemMessage('You are a helpful software architect.');

// 4. Stream response
const stream = session.step({ messages: [
    createUserMessage({ provider: SELECTED_PROVIDER, content: 'Explain why stateless resend improves agent reliability.' })
] });

for await (const event of stream) {
    if (event.type === 'text') {
        process.stdout.write(event.content);
    }
}

4. Wire and Telemetry Mechanics

The client sends a normalized JSON payload matching the provider wire format (OpenAI /v1/chat/completions, Google generateContent, Anthropic /v1/messages). During the stream, Server-Sent Events (SSE) are parsed into standardized events: 'text' (content delta), 'thought' (reasoning trace), and 'quota' (token usage).

5. Run the Example

Execute the runnable example directly from the repository root:

node examples/01_quickstart.mjs

Read the full example: examples/01_quickstart.mjs →

Expected Output: The script connects to your selected provider, logs active options, streams the response token by token to stdout, and concludes with a token usage report (~45 prompt tokens, ~80 completion tokens) and latency benchmark.