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.

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: