LatentKit

Python SDK

Official Python client for the LatentKit /v1 API with sync and async support.

Route-based client for Python 3.10+.

latentkit 0.2.1 is available on PyPI. This release adds the typed /v1/me connection context used across official integrations, including the assigned route, ordered models, and latest winning request.

Use this SDK inside server-side Python code: FastAPI, Django, Flask, background workers, scripts, and scheduled jobs.

Do not put a raw LatentKit API key in frontend code, notebooks that will be shared publicly, screenshots, or AI prompts. Load it from environment variables or a secret manager.

Install

pip install latentkit

The official package is published as latentkit on PyPI.

1. Set your API key

Create a runtime key in AI Router or API Keys, then set it server-side:

export LATENTKIT_API_KEY="lk_..."

2. Sync usage

import os
from latentkit import LatentKit, LatentKitAPIError

with LatentKit(api_key=os.environ["LATENTKIT_API_KEY"]) as client:
    try:
        response = client.chat.create(
            messages=[{"role": "user", "content": "Say hello from LatentKit."}],
            max_tokens=100,
            response_profile="balanced",
        )
        print(response["content"])
    except LatentKitAPIError as exc:
        print({
            "status": exc.status_code,
            "code": exc.code,
            "request_id": exc.request_id,
        })

3. Async usage

import asyncio
import os
from latentkit import AsyncLatentKit

async def main() -> None:
    async with AsyncLatentKit(api_key=os.environ["LATENTKIT_API_KEY"]) as client:
        response = await client.completions.create(
            prompt="Write a one-line description of LatentKit.",
            system="Respond in plain English.",
        )
        print(response["content"])

asyncio.run(main())

Client options

OptionDescription
api_keyRequired API key
base_urlDefaults to https://ai.latentkit.com
timeoutDefault 120.0 seconds
headersExtra request headers
http_clientCustom httpx client

Route-based requests

Do not pass model, provider, route, or policy. The SDK rejects route-control keys. model in responses is reporting metadata for the winning route, not an input field.

Workspace admins change provider and model selection in the console route. Python code sends the task body and optional response_profile.

Inspect the connection and route

Sync and async client.me.retrieve() return the exported MeResponse TypedDict. It includes the app and workspace, credits, assigned route, ordered route models, and latest winning request when available:

context = client.me.retrieve()
route = context.get("policy")

if route:
    print(route.get("name"), route.get("model_count"))
    for model in route.get("models", []):
        print(model.get("rank"), model.get("provider"), model.get("model"))

latest = context.get("latest_request")
if latest:
    print(latest.get("model"))

The latest winner is request activity, not a fixed model. SDK credentials can inspect this runtime context but cannot change the assigned route.

Streaming

with LatentKit(api_key=os.environ["LATENTKIT_API_KEY"]) as client:
    for event in client.chat.stream(
        messages=[{"role": "user", "content": "Count from one to five."}],
    ):
        if event.event == "error":
            raise RuntimeError(event.data)
        if event.is_done:
            break
        print(event.data.get("delta", ""), end="")

Supported resources

Same surface as the JavaScript SDK: chat, completions, vision, embeddings, image, speech, transcription and translation, video, and queue.

The queue helper accepts the complete public enqueue endpoint set and optional idempotency_key. It rejects route-control keys in the queued payload and is enqueue-only because the API has no public per-job result route.

Audio transcription

transcript = client.transcription.create(
    audio={
        "base64": "<base64-audio>",
        "media_type": "audio/mpeg",
        "filename": "meeting.mp3",
    },
    language="en",
    response_format="json",
)

print(transcript["content"])

The API key's route must contain an audio_input model. For OpenAI BYOK routes, a workspace admin can enable transcription models such as gpt-4o-transcribe, gpt-4o-mini-transcribe, gpt-4o-transcribe-diarize, or whisper-1.

See also JavaScript SDK and Streaming.

SDK vs CLI

ToolUse it for
latentkit Python packagePython app code that sends AI requests
@latentkit/cliTerminal login, smoke tests, logs, traces, routes, keys, config checks, and CI

Install the CLI separately when you want terminal workflows:

npm install -g @latentkit/cli
latentkit login
latentkit whoami

Framework notes

  • FastAPI: prefer AsyncLatentKit inside async routes and validate payloads with Pydantic.
  • Django: wrap the SDK in a service module or DRF APIView and keep LATENTKIT_API_KEY in server settings.
  • Flask: create the client inside request handlers or an app-managed lifecycle so HTTP clients are closed cleanly.

On this page