Skip to content

Conversation

oesteve
Copy link
Contributor

@oesteve oesteve commented Oct 7, 2025

Q A
Bug fix? no
New feature? yes
Docs? no
Issues Fix #753
License MIT
$options = [
    'stream' => true,
    'stream_options' => [
        'include_usage' => true,
    ],
];

$stream = $agent->call($messages, $options)->getContent();

if (!is_iterable($stream)) {
    throw new \RuntimeException('Invalid result type, expected iterable');
}

foreach ($stream as $chunk) {
    if (is_string($chunk)) {
        $context->setDelta($chunk);
        continue;
    }

    if ($chunk instanceof TokenUsage) {
        $context->addUsedTokens(
            $chunk->totalTokens ?: 0,
            $chunk->cachedTokens ?: 0
        );
    }
}

@OskarStark OskarStark added Platform Issues & PRs about the AI Platform component Agent Issues & PRs about the AI Agent component labels Oct 8, 2025
@carsonbot carsonbot changed the title Add stream usage support for OpenAI GPT [Agent][Platform] Add stream usage support for OpenAI GPT Oct 8, 2025
@OskarStark OskarStark changed the title [Agent][Platform] Add stream usage support for OpenAI GPT [Agent][Platform][OpenAI] Add stream usage support Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Agent Issues & PRs about the AI Agent component Platform Issues & PRs about the AI Platform component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Agent][Platform][OpenAI] Add support to include usage on streams

2 participants