Facades
The package provides four Laravel facades that serve as the primary entry points for interacting with LLMs and code agents. Each facade resolves a fresh instance from the service container, so you can chain methods freely without worrying about shared state between calls.StructuredOutput
The primary facade for extracting structured data from unstructured text. Given a response model class (a plain PHP DTO with typed properties), the facade prompts the LLM, validates the response against the model’s type constraints, and returns a fully typed object.Basic Usage
With System Prompt
A system prompt steers the LLM’s behavior for the extraction task. Use it to provide domain-specific instructions or constraints.With Examples (Few-Shot Learning)
Providing input/output examples helps the LLM understand the expected extraction pattern, especially for ambiguous or domain-specific data.Switching Connections
Each call can target a different LLM provider by specifying a connection name that matches an entry in yourconfig/instructor.php connections array.
Fluent API
All configuration can also be set with individual fluent methods. This is useful when you build requests dynamically.Return Types
By default,get() returns the deserialized object matching your response model. For simpler extractions, convenience methods cast the result to scalar types.
Available Methods
| Method | Description |
|---|---|
connection(string $name) | Switch to a different configured connection |
fromConfig(LLMConfig $config) | Use an explicit typed LLM config object |
withRuntime(CanCreateStructuredOutput) | Replace the runtime directly (advanced) |
with(...) | Configure extraction with all parameters at once |
withMessages(...) | Set the input messages |
withInput(string|array|object) | Set arbitrary input data |
withResponseModel(string|array|object) | Set the response model class, object, or array schema |
withResponseClass(string) | Set the response model by class name |
withResponseObject(object) | Set the response model by object instance |
withResponseJsonSchema(array|CanProvideJsonSchema) | Set the response model via JSON Schema |
withSystem(string) | Set the system prompt |
withPrompt(string) | Set the user prompt template |
withExamples(array) | Set few-shot examples |
withModel(string) | Override the model for this request |
withOptions(array) | Set additional provider-specific options |
withOption(string, mixed) | Set a single option key |
withStreaming(bool) | Enable or disable streaming |
withCachedContext(...) | Set a cached context for prompt caching |
intoArray() | Deserialize the result as an array |
intoInstanceOf(string) | Deserialize into the given class |
intoObject(CanDeserializeSelf) | Deserialize using a self-deserializing object |
get() | Execute extraction and return the result |
stream() | Execute extraction and return a stream |
response() | Execute and return the full response wrapper |
inferenceResponse() | Execute and return the raw inference response |
StructuredOutputRuntime and then passed via withRuntime(...).
Inference
For raw LLM inference without structured output extraction. Use this when you need free-form text generation, JSON responses, or tool-calling capabilities without the overhead of schema validation and deserialization.Basic Usage
With System Message
Pass aMessages object when you need fine-grained control over the conversation structure.
JSON Response
Request a JSON-formatted response and parse it directly into a PHP array.Switching Connections
Available Methods
| Method | Description |
|---|---|
connection(string $name) | Switch to a different configured connection |
fromConfig(LLMConfig $config) | Use an explicit typed LLM config object |
withRuntime(CanCreateInference) | Replace the runtime directly (advanced) |
with(...) | Configure with all parameters at once |
withMessages(Messages) | Set the messages |
withModel(string) | Override model |
withMaxTokens(int) | Override max tokens |
withTools(ToolDefinitions) | Add tool/function definitions |
withToolChoice(ToolChoice) | Set tool choice strategy |
withResponseFormat(ResponseFormat) | Set response format (e.g., JSON mode) |
withOptions(array) | Set provider-specific options |
withStreaming(bool) | Enable or disable streaming |
withCachedContext(...) | Set a cached context for prompt caching |
withRetryPolicy(...) | Set a custom retry policy |
withResponseCachePolicy(...) | Set response cache behavior |
get() | Execute and return text content |
asJson() | Execute and return raw JSON string |
asJsonData() | Execute and return parsed array |
response() | Return the full response object |
stream() | Return a stream iterator |
Embeddings
For generating text embeddings (dense vector representations). Embeddings are useful for semantic search, clustering, classification, and similarity comparison.Basic Usage
Switching Connections
With Custom Model
Full Response
Theget() method returns the complete response object, which includes both the embedding vectors and usage statistics.
Available Methods
| Method | Description |
|---|---|
connection(string $name) | Switch to a different configured embeddings connection |
fromConfig(EmbeddingsConfig $config) | Use an explicit typed embeddings config object |
withRuntime(CanCreateEmbeddings) | Replace the runtime directly (advanced) |
withInputs(string|array) | Set input text(s) to embed |
withModel(string) | Override the embedding model |
withOptions(array) | Set provider-specific options |
with(...) | Configure with all parameters at once |
first() | Get the first embedding vector |
vectors() | Get all embedding vectors |
get() | Get the full response object with vectors and usage |
AgentCtrl
For invoking CLI-based code agents (Claude Code, Codex, OpenCode) that can execute code, modify files, and perform complex multi-step tasks. The facade provides a builder pattern for configuring agent execution and returns a structuredAgentResponse with the generated output, tool calls, token usage, and cost.
Basic Usage
Agent Selection
Configuration
The facade automatically applies Laravel configuration defaults fromconfig/instructor.php for each agent type. Builder methods override those defaults for a single call.
Streaming
Process output in real-time with streaming callbacks. TheonText, onToolUse, and onComplete callbacks fire as the agent generates output.
Response Object
Session Management
Resume previous sessions for multi-turn agent interactions. The session ID from a previous response lets you continue where you left off.Available Methods
| Method | Description |
|---|---|
claudeCode() | Get Claude Code agent builder |
codex() | Get Codex agent builder |
openCode() | Get OpenCode agent builder |
make(AgentType) | Get agent builder by type |
fake(array $responses) | Create a testing fake |
withConfig(AgentCtrlConfig) | Apply shared typed config |
withModel(string) | Set AI model |
withTimeout(int) | Set execution timeout in seconds |
inDirectory(string) | Set working directory |
withSandboxDriver(SandboxDriver) | Set sandbox isolation driver |
onText(callable) | Register streaming text callback |
onToolUse(callable) | Register tool use callback |
onComplete(callable) | Register completion callback |
resumeSession(string) | Resume a previous session |
execute(string) | Execute and return response |
executeStreaming(string) | Execute with streaming callbacks |
Dependency Injection
Instead of facades, you can inject the underlying service classes directly into your constructors or method signatures. Laravel’s service container resolves them with the same configuration and HTTP client bindings that the facades use.- Better testability — you can mock the injected service or use constructor injection with a fake
- Explicit dependencies — the class signature documents exactly which services it needs
- IDE autocompletion — your editor can provide method suggestions on the typed property
Facade Behavior
All facades proxy to the underlying service classes registered in the container. TheStructuredOutput facade is registered as a non-singleton (bind), so each resolution returns a fresh instance. Inference and Embeddings are registered as singletons. This means you can chain methods on any facade call without side effects: