Skip to main content

When you need a custom provider

Laravel AI SDK supports the major AI services out of the box — OpenAI, Anthropic, Gemini, and Mistral. However, the standard providers cannot handle cases like:
  • An emerging AI service that is not yet officially supported
  • An internal model gateway or billing management layer you want to route requests through
  • An on-premises inference server that uses a proprietary protocol or authentication method
In these cases, implement a custom provider and register it with the SDK’s AiManager to use it through the same API as the standard providers.
Use the built-in driver for OpenAI-compatible APIsAs of SDK 0.9, the openai-compatible driver is included out of the box. For an internal OpenAI-compatible inference server, such as Ollama’s OpenAI-compatible endpoint, you only need to add its configuration to config/ai.php. You do not need to implement a custom provider.

Architecture overview

Two-layer structure

The Laravel AI SDK consists of two layers: providers and gateways. All providers extend the abstract class Laravel\Ai\Providers\Provider and implement feature-specific contracts (interfaces). For multi-step flows that include tool calls, TextGenerationLoop repeatedly invokes the gateway.

Available contracts

Implement only the contracts for the features you want to provide.
In most cases, implementing TextProvider alone is sufficient.

The TextProvider contract

The interface that text generation providers implement (src/Contracts/Providers/TextProvider.php).
The prompt() and stream() implementations can be delegated to existing traits (GeneratesText, StreamsText), so you only need to implement four methods: the three model name methods and textGateway(). TextGenerationLoop manages the multi-step tool loop, so the gateway only processes a single request step.

Implementation example: custom provider

This example registers an inference service with a proprietary API as a provider named my-inference.
1

Create the provider class

Create app/Ai/Providers/MyInferenceProvider.php.
2

Register in AppServiceProvider

Register the provider in the boot method of App\Providers\AppServiceProvider using extend().
3

Add the provider to config/ai.php

Add the values to your .env file as well.
4

Use it from an agent

After registration, specify the provider name in the provider argument of prompt() to use it just like a standard provider.
To use it as the default provider, change the default key in config/ai.php.

Implementing a custom gateway

Services with a proprietary non-OpenAI-compatible API require a custom gateway that implements the StepTextGateway contract.

The StepTextGateway contract

The interface is defined in src/Contracts/Gateway/StepTextGateway.php. A gateway processes one step of a conversation and returns a StepResponse. The calling TextGenerationLoop manages the tool call loop.
The pre-0.9 TextGateway contract and its generateText(), stream(), and onToolInvocation() methods were removed in 0.9. If you have a custom gateway, migrate it to StepTextGateway. Tool call handling from onToolInvocation() now lives in TextGenerationLoop.

Custom gateway implementation example

A skeleton of a simple gateway that sends HTTP requests to a proprietary inference API.
To support tool calls (function calling), include the tool call results in toolCalls from generateTextStep() and set finishReason to FinishReason::ToolCalls. TextGenerationLoop automatically executes the tools and advances to the next step. Refer to AnthropicGateway.php for an implementation example.

Testing

Using Agent::fake()

To test agents that use your custom provider, call the fake method on the agent class. Regardless of whether a custom provider is in use, the fake gateway is set on the provider.
As of 0.9, responses from Agent::fake() pass through the same TextGenerationLoop as real providers. If you configure a fake tool call for an agent that has not registered that tool, the SDK throws a NoSuchToolException.

Mock provider using extend()

You can also use extend() to register a test provider from the container.

OllamaProvider.php — simple provider implementation example

A minimal provider that connects to a local model server. A good starting point for custom provider implementations.

AnthropicGateway.php — gateway implementation example

A gateway implementation of StepTextGateway, including generateTextStep() and generateStreamStep().

StepTextGateway Contract

The interface definition that text generation gateways must implement.

TextProvider Contract

The interface definition that text generation providers must implement.
Last modified on July 11, 2026