Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openrouter/llms.txt

Use this file to discover all available pages before exploring further.

OpenRouterTextModel handles text generation by POSTing to /chat/completions on the OpenRouter API. It is built on top of the shared OpenAI-compatible wire adapter, so any model exposed through OpenRouter’s chat completions endpoint works out of the box.

Basic usage

Pass a model identifier to OpenRouter::model() and chain it with the Generate::text() builder. Call ->run() to execute the request synchronously and receive a TextModelResponse.
use AiSdk\Generate;
use AiSdk\OpenRouter;

$result = Generate::text()
    ->model(OpenRouter::model('openai/gpt-4o'))
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;
Use instructions() to set a system prompt that shapes the model’s behaviour across the entire conversation:
$result = Generate::text()
    ->model(OpenRouter::model('openai/gpt-4o'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;

Response object

Generate::text()->run() returns a TextModelResponse. The fields most commonly used are:
FieldTypeDescription
$result->textstringThe model’s completion text.
$result->usage->inputTokensintNumber of tokens consumed by the prompt.
$result->usage->outputTokensintNumber of tokens in the completion.
$result->providerMetadata['openrouter']['id']stringThe completion ID returned by OpenRouter (e.g. chatcmpl_openrouter).
$result->providerMetadata['openrouter']['model']stringThe model ID echoed back by the API (e.g. openai/gpt-4o).
Example reading all fields:
$result = Generate::text('Hi')
    ->model(OpenRouter::model('openai/gpt-4o'))
    ->run();

echo $result->text;
echo $result->usage->inputTokens;
echo $result->usage->outputTokens;
echo $result->providerMetadata['openrouter']['id'];
echo $result->providerMetadata['openrouter']['model'];

Model selection

OpenRouter model IDs follow the provider/model-name format. Pass the full identifier string to OpenRouter::model():
OpenRouter::model('openai/gpt-4o')
OpenRouter::model() is a static helper that calls OpenRouter::default()->textModel($modelId) on the configured provider instance. You can browse the full list of available model IDs in the OpenRouter model catalogue.

Build docs developers (and LLMs) love