Skip to main content

Custom providers

Custom providers (BYOK — Bring Your Own Key) let you connect to an OpenAI-compatible API endpoint with your own API key. Local providers such as Ollama are also supported.

ProviderConfig

The ProviderConfig class has the following properties.

Basic usage

When using a custom provider, the model parameter is required.
You can also specify it as an array.

Ollama (local provider)

Local providers like Ollama don’t need an apiKey.

Azure OpenAI

For Azure OpenAI, keep the following in mind.
  • Set type to azure (not openai).
  • Set baseUrl to the host only (do not include paths like /openai/v1).

Azure Managed Identity

Use bearerTokenProvider when your provider uses short-lived tokens, such as Azure Managed Identity. The runtime calls the callback on every provider request. The callback receives an array containing providerName and sessionId, and must return the bearer token as a string. The following example calls a token service in your application.
Handle token acquisition, caching, and refresh inside your callback.

API key command

If you use rotating API keys, use apiKeyCommand to run a command before each provider request and fetch the latest key. The command is run by the Copilot runtime, not by this package.
  • apiKeyCommand must be null or a non-blank string. An empty or whitespace-only string throws an InvalidArgumentException before the request is sent. A non-string value in array form also throws an InvalidArgumentException. Passing a non-string value to the ProviderConfig constructor throws a TypeError from PHP’s type checking. Exception messages never include the command.
  • The runtime treats a command failure as a turn failure, and no request is sent to the model.
  • When bearerTokenProvider is set, it takes precedence and apiKeyCommand is not run.
  • Treat the command string as sensitive, like a credential, and don’t write it to logs or exception messages. This package doesn’t log the command or its output either.
  • apiKeyCommand is available only on ProviderConfig (not supported on NamedProviderConfig).

Important notes

The model parameter is required when using a custom provider. Without it, the call errors out.
  • Always use type: 'azure' for Azure endpoints (*.openai.azure.com).
  • Specify baseUrl as the host only. The SDK builds the path automatically.

The onListModels handler

When using a custom provider in BYOK mode, you can configure client->listModels() to invoke a custom handler instead of the CLI server. Configure it with the listModelsUsing() method. Since the client is prone to being initialized before listModels() is called, always set listModelsUsing() before listModels().
Passing null clears the handler and restores the default CLI server behavior.
When listModelsUsing() is configured, listModels() does not need to connect to the CLI server and does not use a cache either.
For the latest updates, see the GitHub repository.
Last modified on October 26, 2026