fix(provider): 补充独立模型调用统计 - #9692
Open
RhoninSeiei wants to merge 8 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/stats.py" line_range="82-91" />
<code_context>
+) -> None:
+ """Persist stats for one direct provider request."""
+ try:
+ usage = response.usage if response and response.usage else TokenUsage()
+ await db.insert_provider_stat(
+ umo=umo,
+ conversation_id=conversation_id,
+ provider_id=_provider_id(provider),
+ provider_model=provider.get_model(),
+ status=_response_status(response),
+ stats={
+ "token_usage": usage.__dict__.copy(),
+ "start_time": start_time,
+ "end_time": end_time,
+ "time_to_first_token": 0.0,
+ },
+ agent_type=agent_type,
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Clarify and stabilize the shape of stored token usage stats
Here `usage.__dict__.copy()` is persisted directly, so any future changes to `TokenUsage` (new internal fields, non-serializable attrs) will silently alter the stored schema or break serialization. To keep the stored shape stable and explicit, either expose and use a `to_dict()` on `TokenUsage`, or construct the dict from the specific fields you intend to persist (input/output/total tokens, etc.).
Suggested implementation:
```python
stats={
"token_usage": usage.to_dict(),
"start_time": start_time,
"end_time": end_time,
"time_to_first_token": 0.0,
},
```
You’ll need to implement a `to_dict()` method on the `TokenUsage` class (wherever it is defined) that returns a stable, explicit shape, e.g.:
- Only include the fields you intend to persist (for example: `input_tokens`, `output_tokens`, `total_tokens`, `cached_tokens`, etc.).
- Avoid non-serializable attributes or internal fields that might change over time.
For example:
```python
@dataclass
class TokenUsage:
prompt_tokens: int = 0
completion_tokens: int = 0
total_tokens: int = 0
def to_dict(self) -> dict[str, int]:
return {
"prompt_tokens": self.prompt_tokens,
"completion_tokens": self.completion_tokens,
"total_tokens": self.total_tokens,
}
```
Adjust the exact fields to match your current `TokenUsage` definition and what you want to persist long-term.
</issue_to_address>
### Comment 2
<location path="tests/unit/test_star_context.py" line_range="150-159" />
<code_context>
assert config.provider_settings is provider_settings
assert config.provider_settings["fallback_chat_models"] == ["fallback-provider"]
+ @pytest.mark.asyncio
+ async def test_woke_main_agent_persists_one_aggregated_provider_stat(
+ self,
</code_context>
<issue_to_address>
**suggestion (testing):** Add tests for error scenarios in `llm_generate` to validate `ProviderStat.status` and usage when the provider fails or returns an error response.
Since stats are recorded in a `finally` block and status is derived from the LLM response, it would be good to cover non-happy paths too. Please add at least:
1. A case where `StatsProvider.text_chat` raises (e.g. `RuntimeError`), asserting via `pytest.raises` that:
- the exception is propagated,
- a `ProviderStat` is still persisted,
- it has `status == "error"` and zero (or default) token usage.
2. A case where `StatsProvider.text_chat` returns an `LLMResponse` with `role="err"`, asserting that:
- `llm_generate` returns this response,
- the `ProviderStat` has `status == "error"` while preserving the usage values.
This will help ensure stats remain correct for failure and error-response scenarios, which downstream reporting depends on.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
added 4 commits
August 15, 2026 04:11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题背景
AstrBot 原有统计由常规 Agent 执行过程写入
provider_stats。以下调用方式没有经过该统计过程:Context.llm_generate()发起的单次模型请求。Context.tool_loop_agent()创建的独立工具循环。这些请求虽然能够正常返回,但 Dashboard 中的模型调用次数和 Token 总量会低于实际值。请求抛出异常、返回
role="err"或被取消时,也可能缺少对应状态。启用后备 Provider 时,失败请求的 Token 还可能归到最终使用的 Provider,造成 Provider 归属和用量重复计算。脱敏后的现象示例:
修改内容
internal与provider两类调用,并且仅将completed计入成功次数。input_other、input_cached和output三个公开 Token 字段。修改后行为
error,取消请求保存为aborted。role="err"的响应保留服务端返回的 Token 用量。验证