StructuredPromptRequestMaterializer is the default and only bundled materializer. It
uses prompt classes and bundled Twig markdown templates. Custom prompt classes are the
supported extension seam.
Prompt Hooks
System Message
Set a system-level instruction that frames the entire extraction task.User Prompt
Add an additional prompt that supplements the input messages. This is useful for providing extraction instructions without mixing them into the data.Examples
Provide input/output examples to guide the LLM’s extraction behavior. Few-shot examples are one of the most effective ways to improve extraction accuracy.Combined Usage
All prompt hooks can be used together in a single request, either through the fluent API or thewith() shorthand.
Using Stringable Objects as Prompts
BothwithSystem() and withPrompt() accept string|\Stringable, so you can pass any object that implements Stringable — such as an xprompt Prompt class — directly, without calling ->render() or (string) yourself. The value is cast to string immediately at the boundary.
Cached Context
For applications that use the same large context across multiple requests (such as a long document or a set of reference materials),withCachedContext() marks content for provider-level prompt caching. This can significantly reduce costs and latency when supported by the provider.
InferenceRequest::cachedContext() so provider-native caching can take effect, while the per-request prompt remains live.
Replacing The Materializer
When prompt-class customization is not enough, implementCanMaterializeRequest and
inject it into the runtime:
Template Engine Integration
If your application needs a more sophisticated prompt management system with variable interpolation, conditional logic, or template libraries, use the companionTemplate class from the cognesy/template package.
Template package supports Twig, Blade, and Arrowpipe. That does not make
the bundled Instructor prompts engine-selectable: the bundled prompt set uses Twig.
Render alternate templates before passing them to StructuredOutput, or supply custom
prompt classes. See the Template package documentation for full details.
Anthropic Prompt Caching
Automatic caching is opt-in through$structuredOutput->withOptions(['cache_control' => ['type' => 'ephemeral']]).
Anthropic advances the breakpoint as the supplied conversation grows.
Use ['type' => 'ephemeral', 'ttl' => '1h'] for a one-hour automatic cache.
For a stable prefix reused across independent requests, use
$structuredOutput->withCachedContext(system: $instructions, ttl: '1h').
Explicit TTL accepts '5m' and '1h'; omission uses the five-minute default.
All context is still transmitted with each request.
Claude Haiku 4.5 requires a 4,096-token prefix; Sonnet 4.5/4.6 require 1,024 tokens
(verified September 6, 2026). Shorter prefixes silently bypass caching.
See Anthropic’s caching guide
for other model/platform thresholds, breakpoint limits, TTL ordering, and pricing.