Skip to content

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

perf: cache model not found (#6148)

Max Deichmann008045c771ec
diff --git a/packages/shared/src/server/ingestion/modelMatch.ts b/packages/shared/src/server/ingestion/modelMatch.ts
index bab5ed32fa..91dea29fa1 100644
--- a/packages/shared/src/server/ingestion/modelMatch.ts
+++ b/packages/shared/src/server/ingestion/modelMatch.ts
@@ -21,12 +21,17 @@ export async function findModel(p: ModelMatchProps): Promise<Model | null> {
logger.debug(`Finding model for ${JSON.stringify(p)}`);
const redisModel = await getModelFromRedis(p);
if (redisModel) {
- span.setAttribute("matched_model_id", redisModel.id);
span.setAttribute("model_match_source", "redis");
- logger.debug(
- `Found model name ${redisModel?.modelName} (id: ${redisModel?.id}) for project ${p.projectId} and model ${p.model}`,
- );
+ if (redisModel === NOT_FOUND_TOKEN) {
+ return null;
+ } else {
+ logger.debug(
+ `Found model name ${redisModel?.modelName} (id: ${redisModel?.id}) for project ${p.projectId} and model ${p.model}`,
+ );
+ span.setAttribute("matched_model_id", redisModel.id);
+ }
+
return redisModel;
}
@@ -45,6 +50,11 @@ export async function findModel(p: ModelMatchProps): Promise<Model | null> {
span.setAttribute("model_cache_set", "false");
} else {
span.setAttribute("model_match_source", "none");
+
+ if (env.LANGFUSE_CACHE_MODEL_MATCH_ENABLED === "true") {
+ await addModelNotFoundTokenToRedis(p);
+ span.setAttribute("model_cache_set", "true");
+ }
}
logger.debug(
@@ -55,7 +65,9 @@ export async function findModel(p: ModelMatchProps): Promise<Model | null> {
);
}
-const getModelFromRedis = async (p: ModelMatchProps): Promise<Model | null> => {
+const getModelFromRedis = async (
+ p: ModelMatchProps,
+): Promise<Model | typeof NOT_FOUND_TOKEN | null> => {
if (env.LANGFUSE_CACHE_MODEL_MATCH_ENABLED === "false") {
return null;
}
@@ -65,6 +77,9 @@ const getModelFromRedis = async (p: ModelMatchProps): Promise<Model | null> => {
const redisModel = await redis?.get(key);
if (redisModel) {
recordIncrement("langfuse.model_match.cache_hit", 1);
+ if (redisModel === NOT_FOUND_TOKEN) {
+ return null;
+ }
const model = redisModelToPrismaModel(redisModel);
return model;
}
@@ -121,6 +136,25 @@ export async function findModelInPostgres(
return foundModels[0] ?? null;
}
+const NOT_FOUND_TOKEN = "LANGFUSE_MODEL_MATCH_NOT_FOUND" as const;
+
+const addModelNotFoundTokenToRedis = async (p: ModelMatchProps) => {
+ try {
+ const key = getRedisModelKey(p);
+ await redis?.set(
+ key,
+ NOT_FOUND_TOKEN,
+ "EX",
+ env.LANGFUSE_CACHE_MODEL_MATCH_TTL_SECONDS,
+ );
+ } catch (error) {
+ logger.error(
+ `Error adding model not found token for ${JSON.stringify(p)} to Redis`,
+ error,
+ );
+ }
+};
+
const addModelToRedis = async (p: ModelMatchProps, model: Model) => {
try {
const key = getRedisModelKey(p);
diff --git a/worker/src/__tests__/modelMatch.test.ts b/worker/src/__tests__/modelMatch.test.ts
index dce9a54613..3dbf26e331 100644
--- a/worker/src/__tests__/modelMatch.test.ts
+++ b/worker/src/__tests__/modelMatch.test.ts
@@ -67,6 +67,33 @@ describe("modelMatch", () => {
expect(result).toEqual(mockModel);
});
+
+ it("should cache not found models in Redis", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+ const nonExistentModel = "nonexistent-model";
+
+ // First lookup should check Postgres and cache the not-found result
+ const result1 = await findModel({
+ projectId,
+ model: nonExistentModel,
+ });
+ expect(result1).toBeNull();
+
+ // Second lookup should use the cached not-found result
+ const result2 = await findModel({
+ projectId,
+ model: nonExistentModel,
+ });
+ expect(result2).toBeNull();
+
+ // Verify the not-found token exists in Redis
+ const redisKey = getRedisModelKey({
+ projectId,
+ model: nonExistentModel,
+ });
+ const cachedValue = await redis?.get(redisKey);
+ expect(cachedValue).toBe("LANGFUSE_MODEL_MATCH_NOT_FOUND");
+ });
});
describe("findModelInPostgres", () => {