diff --git a/pricing.json b/pricing.json new file mode 100644 index 00000000..f0443799 --- /dev/null +++ b/pricing.json @@ -0,0 +1,27 @@ +{ + "alias0": { + "max_tokens": 128000, + "max_input_tokens": 200000, + "max_output_tokens": 128000, + "input_cost_per_token": 0.000005, + "output_cost_per_token": 0.00005, + "cache_creation_input_token_cost": 0.000005, + "cache_read_input_token_cost": 0.0000005, + "search_context_cost_per_query": { + "search_context_size_low": 1e-2, + "search_context_size_medium": 1e-2, + "search_context_size_high": 1e-2 + }, + "litellm_provider": "openai", + "mode": "chat", + "supports_function_calling": true, + "supports_vision": true, + "tool_use_system_prompt_tokens": 159, + "supports_assistant_prefill": true, + "supports_pdf_input": true, + "supports_prompt_caching": true, + "supports_response_schema": true, + "deprecation_date": "2026-02-01", + "supports_tool_choice": true + } +} \ No newline at end of file diff --git a/src/cai/sdk/agents/models/openai_chatcompletions.py b/src/cai/sdk/agents/models/openai_chatcompletions.py index 40899e5a..f92436b3 100644 --- a/src/cai/sdk/agents/models/openai_chatcompletions.py +++ b/src/cai/sdk/agents/models/openai_chatcompletions.py @@ -14,7 +14,7 @@ import asyncio from collections.abc import AsyncIterator, Iterable from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, Literal, cast, overload -from cai.util import get_ollama_api_base, fix_message_list, cli_print_agent_messages, create_agent_streaming_context, update_agent_streaming_content, finish_agent_streaming, calculate_model_cost +from cai.util import get_ollama_api_base, fix_message_list, cli_print_agent_messages, create_agent_streaming_context, update_agent_streaming_content, finish_agent_streaming, calculate_model_cost, COST_TRACKER from cai.util import start_idle_timer, stop_idle_timer, start_active_timer, stop_active_timer from wasabi import color from cai.sdk.agents.run_to_jsonl import get_session_recorder @@ -1288,8 +1288,12 @@ class OpenAIChatCompletionsModel(Model): total_cost = calculate_model_cost(model_name, total_input, total_output) # Explicit conversion to float with fallback to ensure they're never None or 0 - interaction_cost = max(float(interaction_cost if interaction_cost is not None else 0.0), 0.00001) - total_cost = max(float(total_cost if total_cost is not None else 0.0), 0.00001) + interaction_cost = float(interaction_cost if interaction_cost is not None else 0.0) + total_cost = float(total_cost if total_cost is not None else 0.0) + + # Update the global COST_TRACKER with the cost of this specific interaction + if hasattr(COST_TRACKER, "add_interaction_cost") and interaction_cost > 0.0: + COST_TRACKER.add_interaction_cost(interaction_cost) # Store the total cost for future recording self.total_cost = total_cost diff --git a/src/cai/tools/common.py b/src/cai/tools/common.py index 2650a827..1846d56b 100644 --- a/src/cai/tools/common.py +++ b/src/cai/tools/common.py @@ -1249,7 +1249,7 @@ def run_command(command, ctf=None, stdout=False, # pylint: disable=too-many-arg command, stdout, timeout, - stream=stream, + stream=True, call_id=call_id, tool_name=tool_name, workspace_dir=_get_workspace_dir(), diff --git a/src/cai/util.py b/src/cai/util.py index a33cb05d..c641784e 100644 --- a/src/cai/util.py +++ b/src/cai/util.py @@ -247,7 +247,6 @@ class CostTracker: if os.environ.get("CAI_COST_DISPLAYED", "").lower() == "true": return print(f"\nTotal CAI Session Cost: ${self.session_total_cost:.6f}") - def get_model_pricing(self, model_name: str) -> tuple: """Get and cache pricing information for a model""" # Use the centralized function to standardize model names @@ -256,8 +255,24 @@ class CostTracker: # Check cache first if model_name in self.model_pricing_cache: return self.model_pricing_cache[model_name] + + # Try to load pricing from local pricing.json first + try: + pricing_path = pathlib.Path("pricing.json") + if pricing_path.exists(): + with open(pricing_path, "r", encoding="utf-8") as f: + local_pricing = json.load(f) + pricing_info = local_pricing.get("alias0", {}) + input_cost = pricing_info.get("input_cost_per_token", 0) + output_cost = pricing_info.get("output_cost_per_token", 0) + + # Cache and return local pricing + self.model_pricing_cache[model_name] = (input_cost, output_cost) + return input_cost, output_cost + except Exception as e: + print(f" WARNING: Error loading local pricing.json: {str(e)}") - # Fetch from LiteLLM API + # Fallback to LiteLLM API if local pricing not found LITELLM_URL = ( "https://raw.githubusercontent.com/BerriAI/litellm/main/" "model_prices_and_context_window.json" @@ -280,7 +295,7 @@ class CostTracker: except Exception as e: print(f" WARNING: Error fetching model pricing: {str(e)}") - # Default values if pricing not found + # Default values if no pricing found default_pricing = (0, 0) self.model_pricing_cache[model_name] = default_pricing return default_pricing