
Tiktoken and cost tracking issues on gpt-4o
快速结论:使用 AutoGen GroupChat 实例调用 gpt-4o 模型时,因 tiktoken 版本过旧导致 token 编码失败,同时 cost 计算因 token_count_utils.py 未适配 gpt-4o 而出现异常。优先升级 tiktoken 依赖并检查 cost 配置。
问题场景
用户在使用 AutoGen 框架运行 GroupChat 实例时,调用 OpenAI 新发布的 gpt-4o 模型,触发链式崩溃。此外,token 计数工具 token_count_utils.py 未包含 gpt-4o 的映射,导致 cost 计算失败。
报错原文
openai.InternalServerError: Error code: 500 - {'error': {'message': 'The model produced invalid content. Consider modifying your prompt if you are seeing this error persistently.', 'type': 'model_error', 'param': None, 'code': None}}
ERROR: opspilot.src.group_chat.service: Error code: 500 - {'error': {'message': 'The model produced invalid content. Consider modifying your prompt if you are seeing this error persistently.', 'type': 'model_error', 'param': None, 'code': None}}
Traceback (most recent call last):
...
File ".../autogen/oai/client.py", line 285, in create
response = completions.create(**params)
...
File ".../openai/resources/chat/completions.py", line 581, in create
return self._post(
...
原因分析
可能原因包括:
- 用户使用的 tiktoken 版本较旧,未包含对 gpt-4o 模型 token 编码的支持,导致 GroupChat 运行时 token 计数失败并引发链式错误。
token_count_utils.py中没有定义 gpt-4o 的成本计算规则,导致 cost 追踪功能失效。- 部分场景下,gpt-4o 模型生成的内容被 OpenAI 接口判定为无效,触发 500 错误,可能与会话历史或 prompt 格式有关(非 tiktoken 直接原因)。
环境排查
- 确认 tiktoken 版本是否为最新(支持 gpt-4o 的版本)。
- 确认 AutoGen 版本及其依赖的 openai 库版本是否兼容。
- 检查 Python 版本(用户环境为 Python 3.11)。
- 查看
token_count_utils.py中是否包含 gpt-4o 的条目。
解决步骤
- 升级 tiktoken 到最新版本:在项目依赖中更新 tiktoken 版本(用户可通过手动升级解决)。
- 检查
token_count_utils.py文件,添加 gpt-4o 的成本计算配置(如模型名称、每 token 价格等)。 - 若持续遇到 500 错误,尝试调整 prompt 内容,避免产生无效输出。
- 检查 AutoGen 配置中的模型名称是否与 OpenAI API 中的模型名一致(例如
gpt-4o)。
验证方法
成功运行 GroupChat 实例且不再触发 openai.InternalServerError 或 tiktoken 相关报错;检查 cost 日志确认 gpt-4o 的 token 使用量和费用已正确记录。

![[BUG] `kickoff` hangs when LLM call fails](https://www.chat-gpts.plus/wp-content/uploads/2026/07/1934-e441d0e6-768x403.jpg)

![[BUG] Memory Rag Storage Issue](https://www.chat-gpts.plus/wp-content/uploads/2026/07/1669-60999233-768x403.jpg)