Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

perf: flatten prices in pricingTier cache (#10750)

Hassieb Pakzad927ffa08e1e2
diff --git a/packages/shared/src/server/ingestion/modelMatch.ts b/packages/shared/src/server/ingestion/modelMatch.ts
index a6f1e03ee5..65e0747e93 100644
--- a/packages/shared/src/server/ingestion/modelMatch.ts
+++ b/packages/shared/src/server/ingestion/modelMatch.ts
@@ -1,6 +1,7 @@
import { Model, Prisma } from "../../";
import {
instrumentAsync,
+ instrumentSync,
logger,
recordIncrement,
redis,
@@ -119,17 +120,26 @@ const getModelWithPricesFromRedis = async (
return { model: null, pricingTiers: [] };
}
- const parsed = JSON.parse(redisValue);
+ const parsed = instrumentSync(
+ {
+ name: "parse-redis-model",
+ traceScope: "model-match",
+ },
+ (span) => {
+ span.setAttribute("model-cache-value-length", redisValue.length);
+
+ return JSON.parse(redisValue);
+ },
+ );
- // NEW FORMAT: { model: {...}, pricingTiers: [...] }
if (parsed.model !== undefined && parsed.pricingTiers !== undefined) {
const model = redisModelToPrismaModel(parsed.model);
const pricingTiers: PricingTierWithPrices[] = parsed.pricingTiers.map(
(tier: any) => ({
...tier,
- prices: tier.prices.map((p: any) => ({
- ...p,
- price: new Decimal(p.price),
+ prices: Object.entries(tier.prices).map(([usageType, price]) => ({
+ usageType,
+ price: new Decimal(price as string),
})),
}),
);
@@ -137,27 +147,6 @@ const getModelWithPricesFromRedis = async (
return { model, pricingTiers };
}
- // OLD FORMAT: { model: {...}, prices: [...] } (backwards compatible)
- // Convert old format to new format with pricing tiers
- if (parsed.model !== undefined && parsed.prices !== undefined) {
- const model = redisModelToPrismaModel(parsed.model);
- const pricingTiers = await findPricingTiersForModel(model.id);
-
- // Update cache with new format asynchronously (don't await)
- if (env.LANGFUSE_CACHE_MODEL_MATCH_ENABLED === "true") {
- addModelWithPricingTiersToRedis(p, model, pricingTiers).catch(
- (error) => {
- logger.error(
- `Error updating cache with pricing tiers for ${JSON.stringify(p)}`,
- error,
- );
- },
- );
- }
-
- return { model, pricingTiers };
- }
-
// Unknown format
logger.warn(
`Unknown cache format for model match: ${JSON.stringify(parsed)}`,
@@ -175,6 +164,8 @@ const getModelWithPricesFromRedis = async (
export async function findPricingTiersForModel(
modelId: string,
): Promise<PricingTierWithPrices[]> {
+ if (!modelId) return [];
+
const tiers = await prisma.pricingTier.findMany({
where: { modelId },
include: {
@@ -266,9 +257,19 @@ const addModelWithPricingTiersToRedis = async (
) => {
try {
const key = getRedisModelKey(p);
+
+ const cachedPricingTiers = pricingTiers.map((tier) => {
+ return {
+ ...tier,
+ prices: Object.fromEntries(
+ tier.prices.map((p) => [p.usageType, p.price]),
+ ),
+ };
+ });
+
await redis?.set(
key,
- JSON.stringify({ model, pricingTiers }),
+ JSON.stringify({ model, pricingTiers: cachedPricingTiers }),
"EX",
env.LANGFUSE_CACHE_MODEL_MATCH_TTL_SECONDS,
);