Token tracking y cálculo de costes por mensaje
- Config: COST_PER_1M_INPUT y COST_PER_1M_OUTPUT configurables via .env
- OpenAI adapter: stream_options include_usage para capturar tokens reales
- base.py: acumula input/output tokens de cada iteración del agente
- planner.py: devuelve usage junto con el plan
- engine.py: suma tokens de planner + steps + review, calcula coste USD
- Response incluye usage{input_tokens, output_tokens} y total_cost_usd
Formato compatible con el bridge de Claude Code CLI para integración
con el frontend y reporting a Acai webservice.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -59,6 +59,8 @@ class BaseAgent:
|
||||
)
|
||||
tool_executions: list[ToolExecution] = []
|
||||
accumulated_content = ""
|
||||
total_input_tokens = 0
|
||||
total_output_tokens = 0
|
||||
# Real conversation history: assistant messages + tool results
|
||||
conversation: list[dict[str, Any]] = []
|
||||
tool_fingerprints: dict[str, ToolExecution] = {}
|
||||
@@ -139,6 +141,11 @@ class BaseAgent:
|
||||
tool["parsed_arguments"] = args
|
||||
tool_calls.append(tool)
|
||||
|
||||
# Accumulate token usage from any chunk that has it
|
||||
if chunk.usage:
|
||||
total_input_tokens += chunk.usage.get("input_tokens", 0)
|
||||
total_output_tokens += chunk.usage.get("output_tokens", 0)
|
||||
|
||||
if chunk.finish_reason == "end_turn":
|
||||
break
|
||||
|
||||
@@ -234,6 +241,10 @@ class BaseAgent:
|
||||
"content": accumulated_content,
|
||||
"artifacts": artifacts,
|
||||
"tool_executions": tool_executions,
|
||||
"usage": {
|
||||
"input_tokens": total_input_tokens,
|
||||
"output_tokens": total_output_tokens,
|
||||
},
|
||||
}
|
||||
|
||||
async def _execute_tool(
|
||||
|
||||
Reference in New Issue
Block a user