#2163·MetaGPT

OpenAILLM ignores usage-only streaming chunks with empty choices

Author: cenabCreated Sep 28, 2026Updated Sep 28, 2026

Bug description

At 11cdf466d042aece04fc6cfd13b28e1a70341b1f, OpenAILLM._achat_completion_stream immediately skips every chunk with empty choices. This drops a final usage-only chunk from an OpenAI-compatible endpoint. The parser instead estimates usage locally; for a custom model alias that the token counter does not recognize, the returned text succeeds but the cost manager records zero prompt and completion tokens.

I reproduced this through the unchanged native configuration loader/provider against a local HTTP fixture, using synthetic credentials. The fixture sends these three chunks followed by [DONE] (common id, created, model and object: "chat.completion.chunk" fields omitted below for readability):

json
{"choices":[{"index":0,"delta":{"role":"assistant","content":"Fixture reply."},"finish_reason":null}]}
{"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
{"choices":[],"usage":{"prompt_tokens":4,"completion_tokens":2,"total_tokens":6}}

The native calls, inside an async function, are:

python
from metagpt.config2 import Config
from metagpt.provider.llm_provider_registry import create_llm_instance
from metagpt.utils.cost_manager import CostManager

llm = create_llm_instance(Config.default().llm)
llm.cost_manager = CostManager()
reply = await llm.acompletion_text(
    [{"role": "user", "content": "Explain a programming concept."}],
    stream=True,
)
print(reply)
print(llm.cost_manager.get_costs())
await llm.aclient.close()

The local response text is Fixture reply.. Expected token counts are 4 input and 2 output; observed counts are 0 and 0 for both tested custom aliases. The same fixture's non-streaming response records 4 and 2 correctly. Missing price metadata for custom models is a separate issue; this report only concerns discarded token usage supplied by the server.

Bug solved method

No upstream code has been changed. A focused fix could consume chunk.usage before skipping an empty choices array, with a regression test that supplies a normal content chunk, a stop chunk, then a usage-only chunk. It should retain the existing fallbacks for streams that omit usage and the existing handling of usage attached to a choice. Requesting usage with stream_options where supported is a separate decision; the parser should preserve usage it receives.

Environment information

  • LLM type and model name: openai, tsubasa-fast and tsubasa-pro; local fixture only, no live model request.
  • System version: macOS 27, Apple Silicon; isolated process with unchanged HOME, personal config paths blocked and outbound network limited to loopback.
  • Python version: 3.11.13.
  • MetaGPT version or branch: source at 11cdf466d042aece04fc6cfd13b28e1a70341b1f.
  • packages version: OpenAI 1.64.0, httpx 0.28.1, Pydantic 2.7.4, tiktoken 0.7.0.
  • installation method: source-pinned provider imports in a disposable virtual environment with the required provider dependencies.

Screenshots or logs

usage calculation failed: num_tokens_from_messages() is not implemented for model tsubasa-fast.
nativeUsage: total_prompt_tokens=0, total_completion_tokens=0
non-streaming control: total_prompt_tokens=4, total_completion_tokens=2

This was found while checking a Tsubasa integration. The investigation and this report used AI assistance; no live inference or full MetaGPT workflow success is claimed.

Source: FoundationAgents/MetaGPT