Skip to content

AiTask methods

Fomvasss\AiTasks\Tasks\AiTask. Only modality() and toPayload() are required.

MethodDefaultDescription
modality(): string—text, image, embed, audio or transcription
toPayload(): AiPayload—Builds the request, see AiPayload
name(): stringclass name without Task, snake_caseUsed for routing, dashboard, ai_runs.task, fake responses
setName(string $name): static—Sets the name on an instance
tools(): array[]Laravel\Ai\Contracts\Tool[], see Tools & MCP
toolChoice(): ToolChoice|string|array|nullnullForce a tool call, see Tool choice
maxSteps(): ?intnullStep budget of the tool loop; null — 1.5× the number of tools, at most 25. See Step budget
approvalTtlMinutes(): ?intapprovals.ttl_minutesHow long a pause for tool approval stays resumable; null — no limit. See Resuming
schema(): ?ClosurenullJSON Schema for structured output, see Structured output
viaDrivers(array|string $drivers): static—Driver chain for this instance, see Routing
MethodDefaultDescription
postprocess(AiResponse $response): AiResponse|arrayreturns $responseShapes the response. Runs on every attempt — keep it free of side effects
maxRetries(): int0Retries when isAcceptable() rejects the result, queued path only
isAcceptable(AiResponse|array $result): booltrueWhether the postprocessed result is usable
onCompleted(AiResponse|array $result, bool $attemptsExhausted): voidno-opOnce, for the final result
onFailed(Throwable|string $reason): voidno-opOnce, when the task ends without a result

See Queued tasks.

MethodDefaultDescription
serializeForQueue(): array[]Constructor arguments for rebuilding the task on the worker; also the idempotency source
fromQueueArgs(array $args): staticnew static(...$args)Rebuilds the task
shouldRun(): booltrueLast check before the provider call; false marks the run skipped
jobTimeout(): int300Seconds before the worker kills the job
idempotencyKey(): ?stringhash of tenant, name, modality, argsnull when serializeForQueue() is empty
idempotencyWindow(): ?stringnullPeriod string added to the key; null deduplicates forever
viaQueues(): array[]['request' => ..., 'post' => ...], requires ShouldQueueAi
onQueue(?string $queue): static—Queue for both stages, requires ShouldQueueAi
onConnection(?string $connection): static—Queue connection, requires ShouldQueueAi

| executionContext(): array | [] | Request-only state captured at dispatch and stored with the run (request.execution_context) | | static withExecutionContext(array $context, Closure $call): mixed | runs $call | Applies the context around the provider call, the worker hooks and a retry; restores in finally |

use ActsAsDispatchingUser; implements both for the user and the locale, see Acting as a user.

use SerializesModelsAi; implements serializeForQueue()/fromQueueArgs() for promoted constructor properties, including Eloquent models. See Queued tasks.

MethodDefaultDescription
tenantId(): ?stringnull → TenantResolverTenant the run is billed to
userId(): ?stringnull → auth()->id() at dispatchWho started the run (ai_runs.user_id)
subjectType(): ?stringnullType of the record the run concerns, e.g. order
subjectId(): ?stringnullIts id
defaultMeta(): array[]Goes to context()->meta — visible in the AiTaskStarted event, not stored in ai_runs (use AiPayload::$meta for that)

context(): AiContext returns the resolved tenant, user, name, subject and meta; it’s computed once per instance.

See Budgets & tenants.