Skip to content

Fix missing tokens_used/cost_estimate tracking and force USD display on Cost Estimate#8

Open
AppurvaT27 wants to merge 3 commits into
masterfrom
fix/tokens-used-and-cost-estimate
Open

Fix missing tokens_used/cost_estimate tracking and force USD display on Cost Estimate#8
AppurvaT27 wants to merge 3 commits into
masterfrom
fix/tokens-used-and-cost-estimate

Conversation

@AppurvaT27

@AppurvaT27 AppurvaT27 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Description:

Fixes: #7
Problem:

  • tokens_used and cost_estimate were not being persisted in several status-update paths (failure, awaiting approval, awaiting bench approval), leaving these fields blank or stale even after agent runs completed or failed.
  • cost_estimate is calculated in USD (based on per-model USD pricing), but was displaying with a ₹ symbol due to the site's default currency being INR — even after setting options: "USD" on the field.

Changes

1.executor.py

  • Added calculate_cost_estimate(provider, model, tokens) — computes blended USD cost based on per-model token rates (OpenAI, Gemini, Claude).
  • Wired tokens_used and cost_estimate into all status-update calls in run_planning_phase and run_execution_phase (including failure paths), so cost is tracked consistently at every stage, not just on success.
  • _save_logs() now also persists tokens_used and cost_estimate alongside stage/conversation logs.

2.ai_agent_request.json

  • Added "options": "USD" to the cost_estimate Currency field.

3.ai_agent_request.js

  • Added a DOM-level override in refresh to force the Cost Estimate field to render with a $ prefix, working around a currency-resolution bug where Frappe's currency control was ignoring df.options/df.currency and falling back to the site's System Settings default currency (INR).
  • Minor whitespace cleanup (trailing spaces) in setup_live_log_panel / append_log_entry.

Signed-off-by: AppurvaT27 <appurva.talashilkar@ambibuzz.com>
Signed-off-by: AppurvaT27 <appurva.talashilkar@ambibuzz.com>
Comment thread ampower_koda/agent/executor.py Outdated

DOCTYPE_NAME = "AI Agent Request"

def calculate_cost_estimate(provider: str, model: str, tokens: int) -> float:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provider is unused. The function accepts a provider argument but never uses it

Comment thread ampower_koda/agent/executor.py Outdated
# Blended average rates per 1,000,000 tokens (assumes 3:1 input/output ratio)
rates = {
# OpenAI
"gpt-4o-mini": 0.25,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does exact matches on the model string and falls back to "gpt-4o-mini" for anything else. In practice model names can include versions/suffixes (e.g., "gpt-4o-mini-2025-05-01") so unknown models will silently get the fallback rate.

Comment thread ampower_koda/agent/executor.py Outdated
"claude-3-5-haiku-20241022": 1.00,
}

rate = rates.get((model or "").strip(), rates.get("gpt-4o-mini"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case-sensitivity: keys are compared as-is. The good practice is to normalize to lowercase before matching.

Comment thread ampower_koda/agent/executor.py Outdated
if not tokens:
return 0.0

# Blended average rates per 1,000,000 tokens (assumes 3:1 input/output ratio)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring/comment mentions "assumes 3:1 input/output ratio" but that assumption is not applied anywhere in the function (so it is confusing).

Comment thread ampower_koda/agent/executor.py Outdated
}

rate = rates.get((model or "").strip(), rates.get("gpt-4o-mini"))
return float((tokens / 1_000_000.0) * rate)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the function returns a float produced from arithmetic on floats. For monetary values you should prefer Decimal (or round to cents) to avoid representation/rounding issues when persisting/displaying.

Comment thread ampower_koda/agent/executor.py Outdated
rates = {
# OpenAI
"gpt-4o-mini": 0.25,
"gpt-4o": 5.00,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pricing accuracy: the per-model rates used appear hand-picked. Verify against the official provider pricing; otherwise you may misrepresent the cost.

…stead of using hardcoded rates

Signed-off-by: AppurvaT27 <appurva.talashilkar@ambibuzz.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

tokens_used and cost_estimate fields are not updated in AI Agent Request

2 participants