Quickstart
This quickstart helps you to integrate your LLM application with Langfuse.
Run Langfuse
The fastest way to get started is to use the Langfuse Cloud. Alternatively you can run Langfuse locally to test it out or self-host it.
-
Create account and project on cloud.langfuse.com (opens in a new tab)
-
Copy API keys for your project
.envLANGFUSE_SECRET_KEY="sk-lf-..."; LANGFUSE_PUBLIC_KEY="pk-lf-...";
Add tracing to your backend
npm i langfuse
# or
yarn add langfuse
# Node.js < 18
npm i langfuse-node
# Deno
import { Langfuse } from "https://esm.sh/langfuse"
Example usage, most of the parameters are optional and depend on the use case.
For more information, see the typescript SDK docs.
server.ts
import { Langfuse } from "langfuse";
const langfuse = new Langfuse({
secretKey: process.env.LANGFUSE_SECRET_KEY, // sk-lf-...
publicKey: process.env.LANGFUSE_PUBLIC_KEY, // pk-lf-...
// baseUrl: defaults to "https://cloud.langfuse.com"
});
// Example generation creation
const generation = trace.generation({
name: "chat-completion",
model: "gpt-3.5-turbo",
modelParameters: {
temperature: 0.9,
maxTokens: 2000,
},
prompt: messages,
});
// Application code
const chatCompletion = await llm.respond(prompt);
// End generation - sets endTime
generation.end({
completion: chatCompletion,
});
Capture scores (optional)
Use LangfuseWeb to directly capture user feedback as scores in the browser.
npm i langfuse
Simple feedback component in React:
import { LangfuseWeb } from "langfuse";
export default function UserFeedbackComponent(props: { traceId: string }) {
const langfuseWeb = new LangfuseWeb({
publicKey: process.env.NEXT_PUBLIC_LANGFUSE_PUBLIC_KEY, // pk-lf-...
// baseUrl: defaults to "https://cloud.langfuse.com"
});
const handleFeedback = async (value: number) => {
await langfuseWeb.score({
traceId: props.traceId,
name: "user_feedback", // arbitrary name to identify the type of score
value, // float, optional: scale it to e.g. 0..1
});
};
return (
<>
<button onClick={() => handleFeedback(1)}>👍</button>
<button onClick={() => handleFeedback(0)}>👎</button>
</>
);
}
Explore
Visit Langfuse interface to explore your data.
More information
This is a very short summary of how to get started with Langfuse, have a look at the detailed SDK documentation for features such as:
- Nesting of observations to capture complex chains and agents
- Use of your own existing ids to group traces, observations and scores
- Optimizing asynchronous behavior of the SDK
JS/TS SDK (Edge, Node, Web)→Python SDK→🦜🔗Langchain integration→OpenAI SDK (Python)→API reference→Flowise, Langflow, Litellm, ...→