Skip to content

SDK

The @sypnia/ai package for servers and browsers: every function, its options and what it returns.

Applies to EngineCloud
View as Markdown
On this page
  1. On your server: @sypnia/ai/server
  2. In the browser: @sypnia/ai/browser
  3. Errors
  4. With Telemetry
Terminal
npm install @sypnia/ai

TypeScript, for Node 20 or newer, Bun, Deno and edge runtimes. MIT.

On your server: @sypnia/ai/server

TypeScript
import { createAI } from '@sypnia/ai/server';
const ai = createAI({ apiKey: process.env.SYPNIA_SECRET_KEY! });
OptionDefaultWhat it does
apiKey—the project’s secret key, or a key made for AI
baseUrlhttps://ai.api.sypnia.com/v1your gateway’s /v1 when self-hosted
defaultCase—the case used when a call names none and no trace says it
timeoutMs600000the longest a call may take
retries2retries on network errors and 429 (after retry-after)
FunctionReturns
chat({ task, case, endUser, messages, tools, maxToolRounds, stream, budget, store, params }){ text, message, toolCalls, usage, model, callId, finishReason }; with stream: true an async iterator with final()
extract({ task, case, schema, input }){ data, usage, callId }: data typed from a zod schema or JSON Schema
embed({ task, case, input, dimensions }){ vectors, usage }
browserTokens.create({ endUser, tasks, ttlSeconds }){ token, expiresAt }, at most 600 seconds
usage({ groupBy, since })rows of usage
untrusted(text)the text wrapped as data (Prompt injection)

usage is always { input_tokens, output_tokens, cached_input_tokens, credits }.

In the browser: @sypnia/ai/browser

TypeScript
import { createAI } from '@sypnia/ai/browser';
const ai = createAI({ project: 'la-espiga', getToken: () => data.auth.session() });

chat, extract and embed, for tasks with browser: true only (From the browser). The SDK exchanges and refreshes the browser token by itself.

Errors

Every failure is an AIError with code (as in the API), status, message, callId and, for 429, retryAfter.

TypeScript
try { await ai.chat({ … }); }
catch (e) { if (e instanceof AIError && e.code === 'end_user_limit') return tell('TooManyQuestions'); throw e; }

With Telemetry

When @sypnia/telemetry traces the request, the SDK sends the trace along and reads the current case, so each call appears inside it.

  1. On your server: @sypnia/ai/server
  2. In the browser: @sypnia/ai/browser
  3. Errors
  4. With Telemetry

    Search pages, sections and terms