Skip to main content
The LLM module provides a unified interface for calling large language models from your Python modules. All LLM calls are routed through the Kodexa AI Gateway, which handles provider routing, credential management, rate limiting, and cost tracking centrally.

Architecture

Instead of calling provider APIs directly (OpenAI, Bedrock, Gemini, etc.), all LLM requests go through a single gateway:
This means your module code never needs provider-specific API keys or SDKs. The platform manages all credentials centrally.

Provider Coverage

Every model is called through the same unified request, tool-calling, and streaming interface, whichever provider serves it. Alongside the OpenAI, Anthropic (Bedrock), and Gemini families, the gateway also routes to the DeepSeek (including DeepSeek R1), Qwen 3 (including the vision-capable qwen3-vl family), and Z.ai GLM model families on Amazon Bedrock. Not every model family supports every feature, and the gateway validates per-family limitations up front with a clear error instead of surfacing a raw provider failure mid-request. For example, a tool-calling request against DeepSeek R1 — which does not support tool use — fails immediately with a descriptive error before the request ever reaches the provider.

Capability Flags

The gateway’s model catalog can declare per-model capability flags, surfaced in each model’s metadata on the model list (GET /api/ai/models), so you can pick a model by what it actually supports:
The flags are tri-state: true, false, or omitted entirely when a capability has not been declared for that model.

Streaming Errors

If a streaming call fails after the stream has started, the failure arrives as a structured error event in the stream — using the same error taxonomy as non-streaming calls — rather than as error text spliced into the model’s reply. The stream still terminates with the normal [DONE] sentinel, so consumers that only watch for the sentinel exit cleanly.

Quick Start

ModelManager

ModelManager is a singleton that discovers available models from the platform at runtime. On first use, it queries the platform’s cloud-models API and creates a GatewayModelProvider for each model.

Getting Models

Environment Variables

When running inside a Kodexa module execution, KODEXA_URL and KODEXA_ACCESS_TOKEN are automatically set by the platform. You do not need to configure them manually.

ChatMessage

Represents a message in a conversation with an LLM.

Fields

Invoking Models

Basic Invocation

invoke() sends messages and returns the response synchronously.
Parameters: Returns: Tuple[str, Optional[str], LLMUsageMetrics]
  • str — The response text
  • Optional[str] — Thinking output (if thinking mode enabled and supported)
  • LLMUsageMetrics — Token usage and timing

Async Invocation

Async invocation requires the httpx package. Install it with: pip install httpx

Streaming

stream_invoke() yields text chunks as they arrive from the model, useful for real-time display.

Thinking Mode

Some models (Claude 3.7+, Gemini 2.5+) support extended thinking, where the model shows its reasoning process.

Function Calling / Structured Output

Use invoke_function() to extract structured data using a JSON schema. The model is instructed to call a function with arguments matching your schema.
An async version is also available:

Multimodal Input

Send images alongside text for visual document analysis:
Images are automatically base64-encoded and sent in the OpenAI multimodal format.

PDF Documents

Set media_type="application/pdf" to send a PDF as a native document, letting the model read the whole file—text and layout—without rasterizing it to page images first.
The gateway sends PDFs as a native document content block rather than an image, so the model receives the document as-is.
PDF input is supported only on Bedrock Claude models (Claude 3.5 and later) and Google / Vertex AI Gemini models. Other providers—including Azure OpenAI—do not accept PDF documents; a PDF sent to an unsupported model is silently dropped and only the accompanying text messages reach the model. Use a PDF-capable model (or convert pages to images with media_type="image/...") when targeting other providers.

LLMUsageMetrics

Every invocation returns usage metrics for cost tracking and monitoring.

Cost Tracking

Token usage is automatically recorded in the platform’s model interaction system. Use the note parameter to label interactions for billing visibility:

Capability Checking

Check what a model supports before calling specialized methods:

Complete Module Example

Here’s how to use the LLM module in a Kodexa processing module: