Skip to main content
The official @webamigos/ragen-sdk-ts package wraps the Ragen REST API with fully typed responses, first-class streaming support, automatic retry on 429 and 5xx responses, and convenience helpers for file upload — so you can focus on building your integration rather than wiring HTTP calls by hand. It works in Node.js 18+, edge runtimes (Vercel Edge, Cloudflare Workers), and the browser.

Installation

Configuration

Pass a configuration object when you construct the client. apiKey and baseURL are required (both fall back to environment variables) — everything else has a sensible default.

Initializing the client

If most of your calls target the same assistant, set assistantId on the client once and omit it from every individual call.

Methods

Chat completions are the core of Ragen — they send a message to an assistant, trigger retrieval from the knowledge base, and return a grounded answer.

ragen.chat.completions.create(params)

Returns a single completion object. Use this when you want to wait for the full answer before doing anything with it.

ragen.chat.completions.stream(params)

Returns an async iterable of SSE chunks. Use this to forward tokens to the user as they arrive.

ragen.chat.completions.streamToString(params)

Convenience wrapper that collects the entire streamed response into a single string.

Error Handling

All errors thrown by the SDK extend RagenError, which carries a status code, a code string, and a human-readable message. Pattern-match on the subclass to handle specific failure modes: The SDK automatically retries 429 and 5xx responses with exponential backoff and jitter, up to maxRetries times (default 2). When the SDK throws RagenRateLimitError or RagenAPIError, it has already exhausted all retries.

Next.js App Router Streaming

A common pattern is to proxy the SDK stream directly to the browser from an App Router Route Handler. The example below runs on the edge runtime and streams tokens as plain text:
app/api/chat/route.ts

Using the Raw OpenAI Client

If you are working in Python, Go, or an environment where you already have the openai package installed, you can point the OpenAI client at your Ragen instance and use it directly — the chat completions endpoint is wire-compatible.
The TypeScript SDK (@webamigos/ragen-sdk-ts) is the recommended integration path. It provides type safety, auto-retry, streaming helpers, and file upload utilities that aren’t available through the raw OpenAI client.