Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] litellm cost_per_token not being calculated #2288

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions agenta-backend/agenta_backend/core/observability/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,15 @@ def calculate_costs(span_idx: Dict[str, SpanDTO]):
and span.meta
and span.metrics
):
model = span.meta.get("response.model")
prompt_tokens = span.metrics.get("unit.tokens.prompt", 0.0)
completion_tokens = span.metrics.get("unit.tokens.completion", 0.0)

try:
costs = cost_calculator.cost_per_token(
model=span.meta.get("response.model"),
prompt_tokens=span.metrics.get("unit.tokens.prompt", 0.0),
completion_tokens=span.metrics.get("unit.tokens.completion", 0.0),
call_type=span.node.type.name.lower(),
model=model,
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
)

if not costs:
Expand All @@ -406,5 +409,8 @@ def calculate_costs(span_idx: Dict[str, SpanDTO]):
span.metrics["unit.costs.completion"] = completion_cost
span.metrics["unit.costs.total"] = total_cost

except: # pylint: disable=W0702:bare-except
pass
except: # pylint: disable=bare-except
print("Failed to calculate costs:")
print(
f"model={model}, prompt_tokens={prompt_tokens}, completion_tokens={completion_tokens}"
)
19 changes: 12 additions & 7 deletions agenta-cli/agenta/sdk/tracing/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,13 +1229,15 @@ def calculate_costs(span_idx: Dict[str, SpanDTO]):
and span.meta
and span.metrics
):
model = span.meta.get("response.model")
prompt_tokens = span.metrics.get("unit.tokens.prompt", 0.0)
completion_tokens = span.metrics.get("unit.tokens.completion", 0.0)

try:
costs = cost_calculator.cost_per_token(
model=span.meta.get("response.model"),
prompt_tokens=span.metrics.get("unit.tokens.prompt", 0.0),
completion_tokens=span.metrics.get("unit.tokens.completion", 0.0),
call_type=span.node.type.name.lower(),
response_time_ms=span.time.span // 1_000,
model=model,
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
)

if not costs:
Expand All @@ -1248,5 +1250,8 @@ def calculate_costs(span_idx: Dict[str, SpanDTO]):
span.metrics["unit.costs.completion"] = completion_cost
span.metrics["unit.costs.total"] = total_cost

except:
pass
except: # pylint: disable=bare-except
print("Failed to calculate costs:")
print(
f"model={model}, prompt_tokens={prompt_tokens}, completion_tokens={completion_tokens}"
)
Loading