AI Models

Related Documentation
Minimum Version
AI Gateway - 2.0
Incompatible with
on-prem
Previous Versions of this page

What is an AI Model?

The AI Model entity gives clients a single endpoint for one or more upstream LLMs through AI Gateway. Callers don’t need to know which AI Provider or model handles the request. Use AI Models to:

An AI Model declares which capabilities it exposes (like generate or embeddings), which upstream LLM models it routes to via AI Model Providers, and how requests are distributed and logged. Consumer authentication is configured through AI Auth Strategies on the model.

Manage AI Models

AI Models can be created and managed through:

  • Konnect UI
  • AI Gateway API: /v1/ai-gateways/{aiGatewayId}/models
  • kongctl

For configuration examples and step-by-step setup instructions, see Set up an AI Model.

How it works

At request time, the AI Model mediates traffic between clients and upstream AI Provider APIs:

  1. Translates between the request and response format chosen for the AI Model and the upstream AI Provider’s native format.
  2. Resolves upstream connection coordinates (protocol, host, port, path, HTTP method) from the selected target and its AI Model Provider, unless the target is a self-hosted model.
  3. Authenticates to the upstream LLM service using credentials stored on the AI Model Provider entity.
  4. Decorates the upstream request with per-target configuration (such as temperature or token-limit overrides) declared on targets[].config.
  5. Records usage statistics (tokens, cost, latency) for attached log AI Policies, and optionally the full request and response when payload logging is enabled.
  6. Fulfills requests to self-hosted models using the supported native format transformations.

A single AI Model can expose multiple upstream AI Providers behind a consistent client-facing format, so callers don’t change their request shape when the underlying AI Provider changes.

Model lifecycle

When you create or update an AI Model, AI Gateway provisions the necessary runtime resources and applies the configuration atomically. Credentials are sourced from the AI Model Provider entity that the AI Model’s targets reference at model creation time. If you update the AI Model Provider’s credentials later, those changes automatically propagate to all AI Models that use it.

An AI Model is a managed entity. AI Gateway owns its runtime configuration. Direct modifications through other APIs are not supported. To change an AI Model’s configuration, update the AI Model entity directly.

Capabilities

When you expose an AI Model, you choose which AI capabilities it provides through the capabilities field. The type you select determines which capabilities are available:

  • model type: for synchronous request/response workloads. Available capabilities: generate, agentic, embeddings, audio/speech, audio/transcription, audio/translation, image, video, realtime, rerank.
  • api type: for asynchronous batch processing. Available capabilities: batches, files.

Not every LLM service supports every capability. The set of capabilities you can declare on an AI Model depends on what the AI Model Provider in targets exposes. See AI Gateway providers for per-provider details.

Capability

Default OpenAI path

Description

generate /chat/completions, /completions, /responses Text generation and conversational responses from generative models.
agentic /assistants Persistent tool-using agents with state management and metadata.
embeddings /embeddings Vector representations for semantic search and similarity matching.
audio/speech /audio/speech Text-to-speech synthesis.
audio/transcription /audio/transcriptions Speech-to-text conversion.
audio/translation /audio/translations Audio translation between languages.
image /images/generations, /images/edits Generate or edit images from text prompts.
video /videos Generate videos from text prompts.
realtime /realtime Bidirectional WebSocket streaming for low-latency interactive sessions.
rerank /rerank Rank documents by relevance to a query.
batches /batches Asynchronous bulk LLM requests for long workloads.
files /files File uploads for long documents and structured input.

Request and response formats

By default, AI Models expose all endpoints using OpenAI-compatible format. AI Gateway provides a single, standardized interface across all providers, so you can swap providers (OpenAI, Anthropic, self-hosted, etc.) without changing client code or integration logic.

The formats array lets you control the request and response format. Each entry has a type that selects the format. The default openai format translates upstream provider responses into the OpenAI shape, so clients use one API format regardless of provider.

If you need the provider’s native format instead, set formats[].type to a non-OpenAI value. The AI Model passes requests upstream without conversion, while AI Gateway continues to provide analytics, logging, and cost calculation. You can also customize the endpoint paths through config.route.paths if needed.

Format

Provider

Native capabilities

openai All supported providers (default) Translates between OpenAI request and response shapes and the upstream provider format.
anthropic Anthropic Messages, batch processing.
bedrock Amazon Bedrock Converse, RAG (RetrieveAndGenerate), reranking, async invocation.
cohere Cohere Reranking.
gemini Gemini (Standard and Enterprise) Content generation, embeddings, batches, file uploads, reranking, long-running predictions.
huggingface Hugging Face Text generation, streaming.

When a native format is set, only the corresponding provider is supported with its specific APIs.

Targets

An AI Model is a virtual model: it exposes a single config.route configuration and one set of capabilities, then distributes requests across one or more concrete upstream models declared in its targets array. Each entry represents a single upstream model instance with one URL.

For each target, you provide the upstream model name (for example, gpt-4o) and reference the AI Model Provider to use by its name. Each target can also override settings such as temperature, max_tokens, input_cost, and output_cost. For providers with cache, context-window, or service-tier pricing, a target also accepts cache_read_cost, cache_write_cost, cache_write_cost_list, context_window_factor, and service_tier_factor. See Model cost management for how these fields combine to calculate a request’s cost.

There’s no separate target entity or endpoint. Targets are managed only as nested data inside an AI Model, through the same AI Model API surface used to create, update, and delete the parent. Adding, removing, or modifying a target is an update to the AI Model itself.

Load balancing

An AI Model routes to a single target by default. You can add more than one target when you want redundancy, fallback between providers, or cost and latency optimization. When you have multiple targets, configure config.balancer to distribute requests according to a load balancing algorithm.

When an AI Model has more than one target, the load balancer sits between the virtual model and its targets, distributing requests according to config.balancer. For algorithm details, selection guidance, and tuning, see Load balancing.

Algorithms

The algorithm field lets you choose how to distribute requests across target models based on your priorities. Select a strategy to optimize for cost, latency, even distribution, intelligent routing, or failover behavior.

Algorithm

Behavior

round-robin Weighted traffic distribution across targets.
consistent-hashing Sticky sessions based on header values.
least-connections Route to backends with spare capacity.
lowest-latency Route to the fastest-responding model.
lowest-usage Route based on token counts or cost.
semantic Route based on prompt-to-model similarity.
priority Tiered failover across model groups.

Retry and fallback

To add redundancy and failover, the load balancer supports configurable retries, timeouts, and failover to different targets when one is unavailable. Fallback works across targets with any supported format, so you can mix providers freely (for example, OpenAI and Mistral). For configuration details, see Retry and fallback configuration.

Client errors don’t trigger failover. To fail over on additional error types, set failover_criteria to include HTTP codes like http_429 or http_502, and non_idempotent for POST requests.

Forward proxy support

Set config.proxy on this entity to route its outbound requests through an HTTP forward proxy. Use this in network-isolated deployments where AI Gateway cannot open direct connections to LLM providers or auxiliary services.

The proxy record is identical for AI Model, AI MCP Server, and supported AI Policy entities. Existing capabilities such as load balancing, health checking, streaming, WebSocket, and HTTP/2 continue to work when the proxy is active.

For the full field reference, traffic flow, and limitations, see Forward proxy support.

Health check and circuit breaker

To improve reliability under sustained failures, the load balancer includes a circuit breaker. When a target reaches the failure threshold set by max_fails, the load balancer stops routing requests to it until the fail_timeout period elapses. For behavior examples and tuning, see Circuit breaker.

Vector store

To route requests based on semantic similarity and keep similar requests on the same model instance, you can use a vector store. This is useful for caching consistency, routing to specialized model variants, or matching requests against historical patterns.

A vector store holds numerical representations (embeddings) of requests and responses so the runtime can match new requests against stored vectors. It powers the semantic algorithm and any similarity-matching workflow on the AI Model. Configure storage through config.balancer.vectordb by selecting a type:

Type

Connection details

redis Connects to Redis with Vector Similarity Search (VSS), AWS MemoryDB for Redis, or Valkey. AI Gateway auto-detects Valkey from the server name field and uses the Valkey-specific driver.
pgvector Connects to PostgreSQL with the pgvector extension.

For deeper background on vector storage and similarity matching, see Embedding-based similarity matching.

Embeddings

Configure an embedding model to enable semantic routing. This lets AI Gateway route requests based on meaning and content similarity rather than just cost or latency. For example, route domain-specific queries to specialized providers or keep similar requests on the same provider for consistency.

Set config.balancer.embeddings to reference an AI Model Provider and embedding model name. Supported provider types: azure, bedrock, gemini, huggingface, mistral, ollama, openai. The embedding model also powers the semantic load balancing algorithm.

Templating

The AI Model resolves runtime values from request data using placeholder substitution. This lets you select the target model dynamically per request, route to per-deployment Azure endpoints, or fan out to multiple providers from a single AI Model.

Substitution applies to the name of each target model and to any per-target config option. Three placeholders are available:

  • $(headers.header_name): the value of a request header.
  • $(uri_captures.path_parameter_name): the value of a captured URI path parameter.
  • $(query_params.query_parameter_name): the value of a query string parameter.

For examples of using templating, consult the AI Gateway documentation and API reference.

Request routing rules

By default, applications or services making requests to the AI Model endpoint must specify the actual upstream model name (like gpt-4o) in the model field. If you want to allow them to use a different name, for abstraction, stability, or to hide implementation details, configure config.route.model with a routing rule.

config.route.model accepts one of the following match strategies:

  • body_param: matches a value in the request body, indexed by property name (for example, body_param: model).
  • header_param: matches a value in the request headers, indexed by header name.
  • path_param: matches a value present in the request path, indexed by path parameter name.

Each strategy pairs with a values array (currently limited to one value) listing the alias that routes to this AI Model. When a routing rule is set, clients can send the alias value (in the body, a header, or the path, depending on the strategy chosen) instead of the upstream model name. This is useful when you want to decouple your client API from upstream provider changes. For example, you could expose an alias like production-chat-model while swapping the underlying upstream model from gpt-4o to claude-3-sonnet without your clients noticing.

When config.route.model isn’t set, AI Gateway creates a default model selector using the AI Model’s name and format.

Access control

To limit which teams or applications can call an AI Model, use the access.acls field to set an allow list or a deny list. Reference AI Consumers (individual applications), AI Consumer Groups (teams), or Authenticated Groups (all consumers authenticated via a specific OAuth2 scope or claim) by name.

To control how consumers authenticate before their access is evaluated, configure the access.auth_strategies array with one or more AI Auth Strategy references. Each AI Model supports one key-auth auth strategy and one openid-connect auth strategy simultaneously.

Attach AI Policies

Attach an AI Policy to an AI Model to add security, observability, governance, rate limiting, and cost optimization to all requests through that model. For example, you can add guardrails (AI Prompt Guard, AI Lakera Guard), enable logging and metrics, audit and compliance controls, cache responses, or rate-limit LLM traffic.

Reference AI Policies through the policies field, which accepts AI Policy names or IDs. You can attach multiple AI Policies to a single AI Model; each applies independently, and the same AI Policy type can be attached with different configurations.

AI Policy execution order

Authentication runs first, through the AI Model’s assigned AI Auth Strategy, not through an attached AI Policy. Attached AI Policies then execute in a defined order based on policy type. If execution order matters for your use case, refer to the plugin priority documentation.

Upstream proxy configuration

When your data plane sits behind a corporate firewall or security boundary, configure a forward proxy to route all outbound AI provider requests through your organization’s proxy. This is required when direct internet access is restricted and all external traffic must pass through a bastion host or inspection gateway.

Use the config.proxy object to specify the proxy endpoint with http_proxy or https_proxy, and optionally add auth credentials if the proxy requires authentication. Use no_proxy to bypass the proxy for specific hosts that are already inside your trusted network.

Logging and observability

AI Gateway automatically records token consumption, request latency, and per-provider costs for every AI Model, with no separate toggle required. This data flows into Konnect analytics and any attached logging AI Policies, letting you monitor API spend, identify slow providers, and audit which AI Models drive the most usage.

Optionally enable payloads to capture full request and response bodies. This is useful for debugging model responses, auditing sensitive operations, or replaying requests.

Payload logging may expose sensitive data in your logging destination. Only enable it when your logging pipeline is prepared to handle request and response bodies, and verify that logging destinations comply with your data residency and privacy policies.

For response streaming behavior, see Streaming.

Set up an AI Model

Before creating an AI Model, first create an AI Model Provider to store credentials for the upstream LLM service.

The following example:

  • Creates an OpenAI Model that exposes the generate capability, routed through a single OpenAI Provider, with token usage logging enabled.
  • References a provider named my-openai-account. Either see Set up an AI Model Provider to create it, or substitute the name of your own AI Model Provider in targets[].provider.

This AI Model proxies client requests to /v1/chat/completions. The base path /v1 comes from config.route.paths, and /chat/completions is appended by the generate capability automatically.

Because config.route.model is set here with body_param: model, requests through this AI Model must send "model": "my-gpt-4o" (the alias) in the request body instead of the upstream target name (gpt-4o). Sending the upstream target name instead of the alias fails.

Schema

FAQs

An AI Model entity is the first-class AI Gateway entity you declare through the Konnect API and UI. It defines routing, capabilities, and load balancing. An AI Policy is a reusable configuration that adds behavior (like caching or guardrails) to an AI Model. You declare both separately and attach AI Policies to AI Models.

AI Gateway deletes the AI Model’s derived primitives and recreates them from the updated entity state, all within a single database transaction. On failure, the transaction rolls back and no partial state is written.

Yes, by attaching one AI Policy with that configuration to each AI Model. AI Policies are not shared between entities, each instance is independent. See AI Policy entity.

Set the access.acls field on the AI Model with an allow list or a deny list. Each entry is a string that references an AI Consumer, AI Consumer Group, or Authenticated Group by name.

No. AI Model Provider credentials live on the AI Model Provider entity and are materialized into the underlying primitives at AI Model creation time. Updating an AI Model Provider propagates the credential change to all AI Models that reference it.

By default, no. The request model field must match the upstream model on one of the AI Model’s targets, otherwise the runtime returns a 400 error. To accept a client-side alias, configure config.route.model with a routing rule. Clients can then send the alias value in the request body, a header, or the path instead of the upstream AI Provider model name. See Request routing rules for details and examples.

Yes. Values for temperature, top_p, and top_k in the request take precedence over the per-target configuration declared on targets[].config.

Exponentially Weighted Moving Average (EWMA). EWMA continuously updates with every response, weighting recent observations more heavily, so older latencies decay over time but still contribute. There is no fixed learning-phase window.

Yes. EWMA ensures every target continues to receive a small share of traffic (typically 0.1% to 5%, depending on the latency gap). This ongoing probing lets the load balancer adapt if a previously slower target becomes faster.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!