Documentation 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 supports multi-step agentic loops where the model can call tools, receive results, and continue reasoning across several back-and-forth cycles before delivering a final answer. This is the foundation for building research agents, function-calling pipelines, and autonomous task runners — all controlled through the single ->maxSteps() method on Generate::text().
How maxSteps Works
By default, ->run() allows only one step: the model responds and the result is returned, even if the model emits tool calls. Setting ->maxSteps(n) raises that ceiling to n cycles.
Generate::text()->prompt(...)->tool(...)->maxSteps(n)->run() is called. The SDK assembles the first TextModelRequest and sends it to the model.TextResult is returned immediately, regardless of how many steps remain.Each tool call in the response is resolved by name, its arguments are validated, and the handler is invoked. Results are collected into
ToolResult objects.An assistant message containing the tool calls, and individual tool messages containing each result, are appended to the conversation. The updated message list is used for the next model call.
If the step counter has not reached
maxSteps, the SDK sends the next request with the extended message history (back to Step 2). Once maxSteps is reached — or the model responds with no tool calls — the final TextResult is assembled from the last response plus all accumulated tool calls and results.Basic Example
search to find the release notes, then call summarise on the content, and finally compose an answer — all within the five-step budget.
Research Agent Example
Here is a fuller agent that usesToolExecutionContext to log every tool invocation:
ToolExecutionContext
When a tool handler declares a ToolExecutionContext parameter as its last argument, the SDK injects it automatically. You do not need to include it in the tool’s input schema — it is always appended after the declared inputs.
$messages array contains the full conversation including all prior tool calls and results for that step, which lets you implement stateful or context-aware tools.
Inspecting Tool Calls and Results
TextResult accumulates every tool interaction across all steps:
| Property | Type | Description |
|---|---|---|
$toolCalls | ToolCall[] | Every tool call made across all steps, in order. |
$toolResults | ToolResult[] | Every tool result, in the same order as $toolCalls. |
Class-Based Tools
Class-based tools work identically with multi-step loops. DeclareToolExecutionContext as the last parameter of __invoke to receive context:
Streaming and Multi-Step
Best Practices
Keep tool descriptions precise
Keep tool descriptions precise
The model decides which tool to call and with what arguments based entirely on the name and description you provide. Vague descriptions like
'Do stuff' lead to incorrect or missing tool calls. Write descriptions as imperative actions: 'Search the web for a query string and return the top results'.Set maxSteps conservatively
Set maxSteps conservatively
Every step is a model API call. Start with a low value (2–4) and raise it only if your task genuinely requires more cycles. Unbounded loops are prevented by the hard
maxSteps ceiling, but unnecessarily high values increase latency and cost on every invocation.Validate tool outputs before returning them
Validate tool outputs before returning them
The SDK passes tool return values directly back to the model as strings. If your tool can return
null, empty strings, or structured data, normalise the output to a meaningful string so the model has actionable information to work with.Use ToolExecutionContext for observability
Use ToolExecutionContext for observability
Injecting
ToolExecutionContext costs nothing and gives you the full message history at each step. Use it to log tool calls, record metrics, or implement circuit-breakers that bail out early if the same tool is called too many times.