$options the generation options every call carries unless it names them itself */ public function __construct(private ProviderInterface $inner, private array $options) {} public function chat(array $messages, array $tools = [], array $options = []): Response { $response = $this->inner->chat($messages, $tools, $options + $this->options); if ($response->usage !== null) { $this->promptTokens += $response->usage->promptTokens; $this->completionTokens += $response->usage->completionTokens; } return $response; } /** * A generator over the inner stream, so each chunk's usage is tallied as it passes; a * caller that stops iterating keeps what had been tallied up to its last chunk. * * @return \Generator */ public function stream(array $messages, array $tools = [], array $options = []): \Generator { $promptBefore = $this->promptTokens; $completionBefore = $this->completionTokens; $prompt = 0; $completion = 0; foreach ($this->inner->stream($messages, $tools, $options + $this->options) as $chunk) { if ($chunk->usage !== null) { $prompt = max($prompt, $chunk->usage->promptTokens); $completion = max($completion, $chunk->usage->completionTokens); $this->promptTokens = $promptBefore + $prompt; $this->completionTokens = $completionBefore + $completion; } yield $chunk; } } public function structured(array $messages, string $schema, array $options = []): mixed { return $this->inner->structured($messages, $schema, $options + $this->options); } public function models(): array { return $this->inner->models(); } public function isAvailable(): bool { return $this->inner->isAvailable(); } public function getModel(): string { return $this->inner->getModel(); } public function withModel(string $model): static { return new self($this->inner->withModel($model), $this->options); } /** * The usage every chat() and stream() call through this provider has reported so far, * summed; structured() reports none. Zero until the first call reports. */ public function usage(): Usage { return new Usage($this->promptTokens, $this->completionTokens, $this->promptTokens + $this->completionTokens); } }