AICredits logo
Integrations

LangFuse

Trace and monitor AICredits API calls with LangFuse. Track latency, cost, and quality for every LLM request.

Use this page with an AI assistant

Opens a new chat with this docs URL and the correct AICredits base URLs.

LangFuse is an open-source LLM observability platform. Add traces to your AICredits requests to monitor latency, token usage, and model output quality in production.

Overview

AICredits already logs every request in your usage dashboard. LangFuse adds a deeper observability layer: distributed traces spanning multiple LLM calls, user session tracking, and human evaluation scores.

LangFuse wraps your OpenAI client with an instrumented version that automatically sends trace data — no code changes beyond the setup.

Setup

pip install langfuse openai
npm install langfuse openai
.env
LANGFUSE_PUBLIC_KEY=pk-lf-...
LANGFUSE_SECRET_KEY=sk-lf-...
LANGFUSE_HOST=https://cloud.langfuse.com

AICREDITS_API_KEY=sk-your-aicredits-key

Automatic Tracing

Use the LangFuse OpenAI wrapper to automatically trace every call:

from langfuse.openai import openai  # Drop-in replacement for openai

client = openai.OpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Summarise the Indian Budget 2025."}],
    name="budget-summary",  # Trace name in LangFuse dashboard
)

print(response.choices[0].message.content)
import { observeOpenAI } from "langfuse";
import OpenAI from "openai";

const client = observeOpenAI(
  new OpenAI({
    baseURL: "https://api.aicredits.in/v1",
    apiKey: process.env.AICREDITS_API_KEY!,
  }),
  { generationName: "budget-summary" }
);

const response = await client.chat.completions.create({
  model: "openai/gpt-4o-mini",
  messages: [{ role: "user", content: "Summarise the Indian Budget 2025." }],
});

console.log(response.choices[0].message.content);

Manual Tracing

For multi-step pipelines, create traces manually to group related LLM calls:

from langfuse import Langfuse
from langfuse.openai import openai

langfuse = Langfuse()
openai_client = openai.OpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

def classify_and_respond(user_message: str, session_id: str) -> str:
    trace = langfuse.trace(
        name="support-request",
        session_id=session_id,
        input=user_message,
    )

    with trace.span(name="classify-intent") as span:
        classification = openai_client.chat.completions.create(
            model="openai/gpt-4o-mini",
            messages=[
                {"role": "system", "content": "Classify the intent as: billing, technical, or general"},
                {"role": "user", "content": user_message},
            ],
            max_tokens=10,
            langfuse_observation_id=span.id,
        )
        intent = classification.choices[0].message.content

    with trace.span(name="generate-response") as span:
        response = openai_client.chat.completions.create(
            model="anthropic/claude-sonnet-4.5",
            messages=[
                {"role": "system", "content": f"You are a {intent} support agent."},
                {"role": "user", "content": user_message},
            ],
            langfuse_observation_id=span.id,
        )
        answer = response.choices[0].message.content

    trace.update(output=answer)
    return answer

Scores & Evaluation

Add quality scores to your traces for offline evaluation:

from langfuse import Langfuse

langfuse = Langfuse()

langfuse.score(
    trace_id="trace-abc123",
    name="helpfulness",
    value=4,
    comment="Clear and accurate response",
)

langfuse.score(
    trace_id="trace-abc123",
    name="factual-accuracy",
    value=0.9,
    data_type="NUMERIC",
)

Combine LangFuse traces with AICredits usage logs for full cost attribution — you can see both the LangFuse trace (which model call was responsible) and the AICredits cost breakdown (how much that model call cost in INR).

On this page