Skip to content
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
118 changes: 118 additions & 0 deletions alembic/versions/c9f3e548adef_add_lambda_model_pricing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""add lambda model pricing

Revision ID: c9f3e548adef
Revises: 39bcedfae4fe
Create Date: 2025-08-25 19:53:57.606298

"""
from csv import DictReader
from decimal import Decimal
import os
from alembic import op
import sqlalchemy as sa
from datetime import datetime, timedelta, UTC


# revision identifiers, used by Alembic.
revision = 'c9f3e548adef'
down_revision = '39bcedfae4fe'
branch_labels = None
depends_on = None


def upgrade() -> None:
# insert the lambda data
effective_date = datetime.now(UTC) - timedelta(days=1)
csv_path = os.path.join(os.path.dirname(__file__), "..", "..", "tools", "data", "lambda_model_pricing_init.csv")
with open(csv_path, "r") as f:
reader = DictReader(f)
rows_to_insert = []
for row in reader:
rows_to_insert.append({
"provider_name": row["provider_name"],
"model_name": row["model_name"],
"effective_date": effective_date,
"input_token_price": Decimal(str(row["input_token_price"])).normalize(),
"output_token_price": Decimal(str(row["output_token_price"])).normalize(),
"price_source": "manual"
})

if rows_to_insert:
connection = op.get_bind()
connection.execute(
sa.text("""
INSERT INTO model_pricing (provider_name, model_name, effective_date, input_token_price, output_token_price, cached_token_price, price_source)
VALUES (:provider_name, :model_name, :effective_date, :input_token_price, :output_token_price, :input_token_price, 'manual')
"""),
rows_to_insert,
)
connection.execute(
sa.text("""
INSERT INTO fallback_pricing (provider_name, model_name, effective_date, input_token_price, output_token_price, cached_token_price, fallback_type)
VALUES (:provider_name, :model_name, :effective_date, :input_token_price, :output_token_price, :input_token_price, 'model_default')
"""),
rows_to_insert,
)

# Fix the cached_token_price for all the other models
csv_path = os.path.join(os.path.dirname(__file__), "..", "..", "tools", "data", "model_pricing_init.csv")
with open(csv_path, "r") as f:
reader = DictReader(f)
rows_to_update = []
for row in reader:
rows_to_update.append({
"provider_name": row["provider_name"],
"model_name": row["model_name"],
"input_token_price": Decimal(str(row["input_token_price"])).normalize(),
"output_token_price": Decimal(str(row["output_token_price"])).normalize(),
"cached_token_price": Decimal(str(row["cached_token_price"])).normalize(),
})

if rows_to_update:
connection = op.get_bind()
connection.execute(
sa.text("""
update model_pricing set cached_token_price = :cached_token_price
where provider_name = :provider_name and model_name = :model_name
"""),
rows_to_update,
)
connection.execute(
sa.text("""
update fallback_pricing set cached_token_price = :cached_token_price
where provider_name = :provider_name and model_name = :model_name
"""),
rows_to_update,
)

# backfill the cached_token_price for all the other models
connection = op.get_bind()
connection.execute(
sa.text("""
with updated_model_pricing as (
update model_pricing set cached_token_price = input_token_price
where cached_token_price = 0
)
update fallback_pricing set cached_token_price = input_token_price
where cached_token_price = 0
"""),
rows_to_insert,
)

# remove the default value for cached_token_price
op.alter_column('model_pricing', 'cached_token_price', server_default=None)
op.alter_column('fallback_pricing', 'cached_token_price', server_default=None)


def downgrade() -> None:
connection = op.get_bind()
connection.execute(
sa.text("""
DELETE FROM model_pricing WHERE provider_name = 'lambda'
"""),
)
connection.execute(
sa.text("""
DELETE FROM fallback_pricing WHERE provider_name = 'lambda'
"""),
)
18 changes: 18 additions & 0 deletions tools/data/lambda_model_pricing_init.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
provider_name,model_name,input_token_price,output_token_price
lambda,deepseek-r1-0528,0.0005,0.00218
lambda,deepseek-v3-0324,0.00034,0.00088
lambda,qwen-3-32b,0.0001,0.0003
lambda,llama-4-maverick-17b-128e-instruct-fp8,0.00018,0.0006
lambda,llama-4-scout-17b-16e-instruct,0.00008,0.0003
lambda,llama-3.1-8b-instruct,0.000025,0.00004
lambda,llama-3.1-70b-instruct,0.00012,0.0003
lambda,llama-3.1-405b-instruct,0.0008,0.0008
lambda,deepseek-llama3.3-70b,0.0002,0.0006
lambda,llama-3.3-70b-instruct,0.00012,0.0003
lambda,llama-3.2-3b-instruct,0.000015,0.000025
lambda,hermes-3-llama-3.1-8b,0.000025,0.00004
lambda,hermes-3-llama-3.1-70b (fp8),0.00012,0.0003
lambda,hermes-3-llama-3.1-405b (fp8),0.0008,0.0008
lambda,lfm-40b,0.00015,0.00015
lambda,llama3.1-nemotron-70b-instruct,0.00012,0.0003
lambda,qwen2.5-coder-32b,0.00007,0.00016
Loading
Loading