LatentKit

JavaScript SDK

Official JavaScript and TypeScript client for the LatentKit /v1 API.

Official ESM package for Node 18+ and other runtimes with fetch, ReadableStream, and AbortController.

Install

npm install @latentkit/sdk

Quickstart

import { LatentKit } from '@latentkit/sdk';

const client = new LatentKit({
  apiKey: process.env.LATENTKIT_API_KEY!,
});

const response = await client.chat.create({
  messages: [{ role: 'user', content: 'Say hello from LatentKit.' }],
  max_tokens: 100,
  response_profile: 'balanced',
});

console.log(response.content);

Client options

OptionDescription
apiKeyRequired API key
baseUrlDefaults to https://ai.latentkit.com (normalized to /v1)
headersExtra request headers
fetchCustom fetch implementation
timeoutMsDefault 120000

Route-based requests

Do not pass model, provider, route, or policy. The SDK rejects route-control keys, including inside extra_body. The assigned route selects the provider/model at runtime.

Errors

import { LatentKit, LatentKitApiError } from '@latentkit/sdk';

try {
  await client.chat.create({
    messages: [{ role: 'user', content: 'hello' }],
  });
} catch (error) {
  if (error instanceof LatentKitApiError) {
    console.error({
      status: error.status,
      code: error.code,
      request_id: error.request_id,
      body: error.body,
    });
  }
}

Return safe errors to your frontend. Do not forward raw provider messages if they may contain internal configuration details.

Streaming

for await (const event of client.chat.stream({
  messages: [{ role: 'user', content: 'Count from one to five.' }],
})) {
  if (event.event === 'error') throw new Error(JSON.stringify(event.data));
  if (event.isDone) break;
  console.log(event.data);
}

Supported resources

  • client.chat.create / client.chat.stream
  • client.completions.create / client.completions.stream
  • client.vision.create / client.vision.stream
  • client.embeddings.create
  • client.image.generate
  • client.speech.create, client.transcription.create, client.translation.create
  • client.video.generate
  • client.queue.create
  • client.agents.sessions.create

See also Python SDK and REST API overview.

Framework notes

  • Next.js: keep the client in a server-only module and call it from Route Handlers or Server Actions.
  • React/Vite: browser code should call your backend or serverless function; it should not import the SDK with a raw key.
  • Express/Fastify/Nest: create one shared client factory, validate incoming request bodies, and centralize LatentKit error logging.

On this page