Skip to content

Migrate mentor notification topic extraction from gpt-4 to gpt-4.1-mini #4671

Closed

Description

@beastoin

Problem

backend/utils/mentor_notifications.py:98-109 hardcodes model="gpt-4" (resolves to gpt-4-0613) for the extract_topics() function. This single call accounts for ~37% of total OpenAI spend with 0% cache hit rate and ~82K requests/30h.

The task is trivial — extract topic keywords from short conversation snippets and return a JSON array. It does not require a frontier model.

Proposed Fix

Replace the raw OpenAI client call with the shared llm_mini langchain client (gpt-4.1-mini), which is ~110x cheaper per input token for this use case.

Before:

from openai import OpenAI
client = OpenAI(...)

def extract_topics(discussion_text: str) -> List[str]:
    response = client.chat.completions.create(
        model="gpt-4",  # resolves to gpt-4-0613, $22/1M input
        messages=[...],
        temperature=0.3,
        max_tokens=150,
    )

After:

from utils.llm.clients import llm_mini  # gpt-4.1-mini

def extract_topics(discussion_text: str) -> List[str]:
    response = llm_mini.invoke(prompt)

This also brings the function in line with codebase conventions (use shared langchain clients, no in-function imports, usage tracking via _usage_callback).

Impact

  • ~35% reduction in total OpenAI spend from a single-line model swap
  • Gains usage tracking via the existing _usage_callback on llm_mini
  • No quality regression expected — topic extraction is a simple structured-output task

Files to Change

  • backend/utils/mentor_notifications.py — replace client.chat.completions.create(model="gpt-4", ...) with llm_mini.invoke(...), remove raw OpenAI client init

Test Plan

  • Run backend/test.sh — all existing tests pass
  • Verify extract_topics() returns valid JSON topic arrays with gpt-4.1-mini
  • Monitor OpenAI dashboard post-deploy for cost drop on gpt-4-0613 line

Activity

  1. added
    intelligence
    maintainer
    p1
    on Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    intelligencemaintainerp1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions