Skip to main content
The TypeScript SDK wraps your existing LLM client instances and automatically captures latency, token counts, costs, and errors for every call — with no changes to your application logic beyond initialization.

Installation

1

Install the package

All provider integrations are optional peer dependencies. Install only what you use:
2

Initialize Zespan

Call zespan.init() once at application startup, before any LLM calls are made.
3

Wrap your LLM client

Pass your client instance through the appropriate wrapper function. The wrapper returns the same client — your existing code continues to work unchanged.

Init options

zespan.init(options) accepts the following configuration:
string
required
Your Zespan API key. Must start with zsp_. Find this in your project settings.
string
default:"production"
Environment label attached to every event. Use "staging" or "development" to separate traces by environment.
boolean
default:"true"
When true (default), prompt and completion text are stored alongside traces with PII redaction applied before transmission. Set to false to disable prompt storage entirely.
number
default:"1.0"
Fraction of events to send, between 0.0 and 1.0. Set to 0.1 to trace 10% of calls.
string[]
Keys whose values are redacted before any data is stored. Passing this option replaces the default list — it is not merged. Applied regardless of storePrompts.
boolean
default:"false"
When true, enables pattern-based PII detection (emails, phone numbers, SSNs, credit cards, and more) on top of key-based redaction. Off by default. See PII redaction for the full configuration options (piiPreset, piiCategories, piiRedactionMode, and others).
number
default:"50"
Number of events to accumulate before flushing.
number
default:"2000"
Milliseconds between automatic flushes. The SDK also flushes on process exit.
string
default:"https://api.zespan.com"
Override the API base URL. Use this only for self-hosted deployments.
boolean
default:"false"
When true, also exports spans to an OpenTelemetry-compatible backend. Requires otelEndpoint.
string
OTLP collector endpoint. Required when enableOTel is true.
string
Service name attached to exported OpenTelemetry spans.
boolean
default:"false"
When true, logs internal errors to the console. Enable during integration testing.
boolean
default:"true"
When true (default), automatically instruments installed OpenAI, Anthropic, and Google (@google/generative-ai and @google/genai) clients as soon as they’re detected — no explicit wrapper call required. Set to false to trace only clients you wrap explicitly. See Automatic instrumentation.
string
Your Zespan project ID. Required to receive config propagation updates from ZespanPilot or the dashboard.
boolean
default:"true"
When false, disables config propagation even if projectId is set — the SDK will not fetch or apply remote config changes.

Provider wrappers

OpenAI

Anthropic

Google Generative AI

This covers the legacy @google/generative-ai package. For Google’s current SDK (@google/genai) — including image generation, embeddings, and Veo video generation — use zespan.wrapGoogleGenAI() instead. See Google Generative AI for the full reference.

AWS Bedrock

wrapBedrock patches both client.converse and client.converseStream, plus client.send() calls that dispatch a ConverseStreamCommand — streaming Bedrock calls are traced the same as non-streaming ones.

Mistral

Groq

OpenRouter

LiteLLM

Automatic instrumentation (autopatch)

By default, Zespan also patches installed OpenAI, Anthropic, and Google (@google/generative-ai, @google/genai) SDKs directly at the module level as soon as zespan.init() runs and the package is detected — no wrapOpenAI-style call required. This is useful for code paths where you can’t easily reach the client instantiation, such as third-party libraries that construct their own client internally.
Set autopatch: false at init to disable this and trace only clients you wrap explicitly. Framework integrations (LangChain, Google ADK) are not affected by this flag — they use their own callback-based instrumentation. If you’re writing a custom integration for a framework Zespan doesn’t support out of the box, call markFrameworkActive(spanId) before your handler’s LLM call fires and markFrameworkInactive(spanId) after, so autopatch doesn’t also enqueue an event for the same call and create a duplicate.

Framework integrations

Beyond direct provider wrappers, Zespan integrates with common agent frameworks. Each has a dedicated reference page with full setup instructions:
  • LangChainZespanCallbackHandler traces chains, agents, tools, and retrievers automatically.
  • Google ADKinstrumentADK, wrapADKRunner, and wrapADKAgent trace Agent Development Kit agents and multi-agent delegation.
  • Vercel AI SDKinstrumentVercelAI() traces generateText, streamText, generateObject, and tool calls via OpenTelemetry.
  • LlamaIndexZespanLlamaIndexHandler attaches to LlamaIndex’s callback manager to trace LLM calls, tool use, and agent steps.
  • CrewAI and AutoGen / AG2 — these frameworks are Python-native. The TypeScript SDK exports context-propagation helpers (extractAgentContext, attachTraceToAutoGenMessage, extractTraceFromAutoGenMessage) so a Node.js service receiving calls from a Python agent can continue the same trace.

Context enrichment

Use withZespanContext to attach a userId, sessionId, or custom tags to all traces generated within a function scope.
Set userId and sessionId on every request that involves a logged-in user. This enables per-user cost breakdown and session replay in the Zespan dashboard.

Agent tracing

Use withAgent to trace a multi-step agent workflow. It creates an agent span and exposes methods to log plans, trace tool calls, and record handoffs.
AgentContext methods:
  • agent.logPlan(steps: string[]) — records a planning span
  • agent.traceTool(name, args, fn) — wraps an async function, records args and result as a tool span
  • agent.delegateTo(targetName, reason) — records a handoff span

Manual spans

Use startSpan to instrument any non-LLM operation (retrieval, embeddings, custom model calls) as a custom span and attach evaluation scores. It returns a span handle and a run helper for propagating context to nested calls.
span.end(options) requires a status ("success", "error", "timeout", "rate_limited", or "cancelled") and is async — always await it. See Manual spans for the full option reference and a complete RAG pipeline example.

Prompt management

The PromptClient fetches versioned prompts from the Zespan prompt library at runtime. Results are cached locally.
See Prompt management for the full API reference — get, list, create, updateLabels, compile, clearCache.

Dataset experiment runs

The client.datasets client lets you run your own pipeline against a Zespan dataset and link the results back by trace ID — then score and compare runs in the dashboard. Fetch a dataset’s items, run each one through your own code however you like, and link each item to a run using the trace ID your own tracing already produced.

datasets.getItems(datasetName)

Lists a dataset’s items by dataset name. Returns Promise<DatasetItem[]> — each item has id, input, expectedOutput, and metadata.
string
required
Name of the dataset to fetch items from.

datasets.createRun(datasetName, runName, options?)

Creates or fetches a named run against a dataset — idempotent, so it’s safe to call every time a job starts. Returns Promise<DatasetRunHandle>.
string
required
Name of the dataset to run against.
string
required
Name for this run, e.g. "gpt-4o-v2". Calling createRun again with the same name returns the existing run instead of creating a duplicate.
string
Optional human-readable description of what this run is testing.
Links a dataset item to the run via a trace ID your own code already produced. Returns Promise<void>.
string
required
id of the dataset item being evaluated, from getItems.
string
required
Trace ID that your pipeline produced for this item — for example, an ID you generated and passed into withZespanContext.
string
Optional span/observation ID, if you want to link to a specific span within the trace rather than the trace as a whole.
Once results are linked, head to Datasets in the dashboard to score and compare runs against each other.

PII redaction

Zespan automatically redacts values from tags and metadata fields (and stored prompt/completion text) before they leave your application. The key is preserved; the value is replaced with "[REDACTED]". Default redacted keys (applied when redactKeys is not set): password, secret, token, api_key. Passing your own redactKeys array replaces the default list — it does not merge with it. Include any of the defaults you still want to keep:
Redaction applies to tags and metadata fields, and to stored prompt and completion text. Prompt storage is on by default — set storePrompts: false to disable it entirely.
For pattern-based detection of PII that doesn’t live under a known key name — emails, phone numbers, SSNs, credit card numbers embedded in free text — set redactPii: true and configure a preset or category list. See PII redaction for the full piiPreset, piiCategories, and piiRedactionMode reference.

Guardrails

Guardrails run content safety checks before sending a prompt to the LLM (pre-check) and before returning the completion (post-check). Pass guardrails: true to any wrapper to enable both phases.
See Guardrails for the full reference — parameter defaults, all fields, the direct checkGuardrails() API, and a multi-wrapper example.

Config propagation

Zespan can push configuration changes — model overrides, sample rate, guardrail toggles — to your running application without a redeployment, as long as projectId is set at init (see Init options). Changes made via ZespanPilot or the dashboard take effect within the next flush cycle (default 2 seconds). See Config propagation for the full list of rule types — including caching, retries, timeouts, fallbacks, and A/B tests — and the programmatic ConfigClient API.

Flushing in serverless environments

In short-lived processes such as AWS Lambda, Vercel Functions, or Cloudflare Workers, call zespan.getClient().flush() explicitly before the handler returns to guarantee delivery.
Omitting zespan.getClient().flush() in serverless environments is the most common cause of missing traces. Always call it before your handler returns.