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
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.mjsRead the full example: examples/01_quickstart.mjs →