LatentKit

Vercel AI SDK

Use LatentKit as a route-based Language Model V3 provider in trusted AI SDK server runtimes.

Published alpha: @latentkit/ai-sdk-provider 0.1.0-alpha.3 is available on npm with signed provenance. The required @latentkit/sdk 0.2.2 release is installed transitively. Review the provider source.

The dedicated LatentKit provider implements the Vercel AI SDK Language Model Specification V3 over the canonical /v1/chat API. It is not an OpenAI-compatible base-URL wrapper.

Use it only in trusted Node.js 22+ server routes, server actions, serverless functions, workers, and backend jobs. Never import it into a browser component or put LATENTKIT_API_KEY in a NEXT_PUBLIC_ variable.

npm install ai @latentkit/[email protected]

Route-based model selection

The AI SDK requires a model identifier. LatentKit accepts only the logical identifier latentkit-route and consumes it locally:

model: latentkit("latentkit-route")

It is never forwarded as a model, provider, route, or policy field. The app key's assigned published route controls provider order, upstream models, fallback, policy, credits, and analytics. Edit that route in the LatentKit console, not application code.

Generate text

The following API is implemented, tested, and available from the exact npm release above.

import { generateText } from "ai";
import { latentkit } from "@latentkit/ai-sdk-provider";

const result = await generateText({
  model: latentkit("latentkit-route"),
  prompt: "Explain the CAP theorem.",
});

console.log(result.text);
console.log(result.providerMetadata?.latentkit?.requestId);

Stream text

import { streamText } from "ai";
import { latentkit } from "@latentkit/ai-sdk-provider";

const result = streamText({
  model: latentkit("latentkit-route"),
  prompt: "Give me three concise deployment checks.",
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}

The provider maps LatentKit SSE into V3 stream start, text lifecycle, tool-input lifecycle, completed tool calls, usage, finish reason, request metadata, structured errors, and clean completion. Cancellation closes the upstream response body, and the default request deadline is 55 seconds.

Next.js route handler

import { latentkit } from "@latentkit/ai-sdk-provider";
import { convertToModelMessages, streamText, type UIMessage } from "ai";

export const runtime = "nodejs";
export const maxDuration = 60;

export async function POST(request: Request) {
  const { messages } = await request.json() as { messages: UIMessage[] };
  const result = streamText({
    model: latentkit("latentkit-route"),
    messages: await convertToModelMessages(messages),
  });

  return result.toUIMessageStreamResponse();
}

Store LATENTKIT_API_KEY only in the deployment's server-side environment.

Function tools

import { generateText, tool } from "ai";
import { z } from "zod";
import { latentkit } from "@latentkit/ai-sdk-provider";

const result = await generateText({
  model: latentkit("latentkit-route"),
  prompt: "What is the weather in Dubai?",
  tools: {
    weather: tool({
      description: "Look up weather by city.",
      inputSchema: z.object({ city: z.string() }),
    }),
  },
});

console.log(result.toolCalls);

Your application executes its functions. LatentKit routes the model call and returns tool calls; it does not run arbitrary application code.

Structured output

AI SDK 7 uses Output.object for typed object generation:

import { generateText, Output } from "ai";
import { z } from "zod";
import { latentkit } from "@latentkit/ai-sdk-provider";

const result = await generateText({
  model: latentkit("latentkit-route"),
  prompt: "Describe the integration status.",
  output: Output.object({
    name: "integration_status",
    schema: z.object({
      name: z.string(),
      ready: z.boolean(),
    }),
  }),
});

console.log(result.output);

Response profile and connection context

Set only the supported LatentKit option:

providerOptions: {
  latentkit: { responseProfile: "fast" },
}

Allowed values are fast, balanced, and deep. Arbitrary provider passthrough is closed. Nested provider/model/route/policy attempts fail before fetch.

The provider factory also exposes the attributed, null-safe /v1/me context:

import { createLatentKit } from "@latentkit/ai-sdk-provider";

const provider = createLatentKit();
const context = await provider.connectionContext();

console.log(context.data.app?.name ?? "App unavailable");
console.log(context.data.policy?.name ?? "No published route");

Supported alpha surface

  • text generation and text streaming
  • system, developer, user, assistant, and tool messages
  • function declarations, tool choice, calls, and result history
  • JSON Schema structured output
  • usage, finish reason, response headers, request IDs, and informational selected provider/model metadata
  • abort, timeout, warnings, standard-fetch edge runtimes, and /v1/me

Embeddings, images, attachments, reasoning parts, provider-defined tools, and provider tool approvals are not supported in this alpha.

Troubleshooting

Missing key: configure an app-scoped LATENTKIT_API_KEY in the trusted runtime. Never move it into browser code.

No such model: use exactly latentkit-route. Configure upstream models in the published LatentKit route.

Timeout: the deadline covers response headers and the active stream. Pass a larger timeoutMs to createLatentKit only if the host permits it.

Request failed: retain the safe LatentKit request ID for support and log correlation. Do not log authorization headers, prompts, generated output, tool arguments, or raw response bodies.

See also the JavaScript SDK, Streaming, and Tool calling guides.

On this page