This guide takes you from a fresh Composer project to working AI generations usingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/core/llms.txt
Use this file to discover all available pages before exploring further.
aisdk/core and the aisdk/openai provider. Each section builds on the one before it, progressing from a minimal text generation example through streaming, tool calling, and structured output.
Complete Working Example
Install the packages
Install both the core SDK and the OpenAI provider with a single Composer command:
Set your API key
The OpenAI provider reads your key from the In a
OPENAI_API_KEY environment variable. Export it in your shell before running your script:.env-driven application (Laravel, Symfony, etc.) add the variable to your .env file and load it through your framework’s normal bootstrap.Generate text
Create a PHP file and call
Generate::text() with a model and a prompt:->run() is synchronous and returns a TextResult value object.Default Model Shortcut
Calling->model(...) on every request is verbose. Register a default model once at application boot and then use the terse single-argument form:
Streaming
Use->stream() instead of ->run() to receive tokens as they arrive. Iterate over the chunks() generator, then call ->run() on the stream to get the final TextResult:
Hooks are chainable and can be attached before or after you start iterating
chunks(). The onFinish callback receives the same TextResult that ->run() returns.Tool Calling
Define a tool withTool::make(), give it an input schema, and attach it to the request with ->tool(). The model calls the tool automatically and the SDK invokes your PHP callable:
Structured Output
Pass aSchema to ->output() to constrain the model’s response to a specific shape. The parsed data is available on $result->output as an associative array:
Next Steps
Text Generation
Deep dive into prompts, system instructions, provider options, and result metadata.
Streaming
Learn all available stream hooks and how to handle errors mid-stream.
Tool Calling
Build complex multi-turn agentic flows with class-based and closure-based tools.
Structured Output
Explore the full
Schema API — objects, strings, numbers, arrays, and enums.