<?php
/** @var array<string, mixed> $optionsBase */
$optionsBase = [
'max_output_tokens' => 4096,
// Improve cache routing for identical prefixes between requests.
'prompt_cache_key' => $cacheKey,
'prompt_cache_retention' => 'in_memory',
];
// get StructuredOutputResponse object to get access to usage and other metadata
$response1 = (new StructuredOutput($jsonSchemaRuntime))
->withCachedContext(
system: $cachedSystem,
prompt: $cachedPrompt,
)
->with(
messages: 'Describe the project in a way compelling to my audience: P&C insurance CIOs.',
responseModel: Project::class,
model: 'gpt-4.1',
options: $optionsBase,
)->create();
// get processed value - instance of Project class
$project1 = $response1->get();
dump($project1);
assert($project1 instanceof Project);
assert($project1->name !== '');
assert($project1->description !== '');
// Extract response id for Responses API server-side chaining
$body1 = json_decode($response1->inferenceResponse()->responseData()->body(), true);
$previousResponseId = is_array($body1) ? ($body1['id'] ?? '') : '';
if ($previousResponseId === '') {
echo "Warning: previous_response_id not available; chaining will be skipped.\n";
}
// get usage information from inferenceResponse() when you need transport-level metadata
$usage1 = $response1->inferenceResponse()->usage();
echo "Usage #1: {$usage1->inputTokens} prompt tokens, {$usage1->cacheWriteTokens} cache write tokens\n";
?>