Connecting your backend LLM events to frontend session replays provides complete visibility into the user journey, helping you understand the full context around AI interactions in your application.
Why link to session replay?
By including session IDs in your LLM events, you can:
- See the full user journey: Navigate from an LLM trace directly to the session replay to see user actions before, during, and after AI interactions
- Debug issues faster: Quickly find and watch the exact session where problems occurred
- Understand user behavior: See how users interact with AI features and what prompts they use
- Correlate performance: Match slow AI responses with actual user experience impact
Implementation
To link LLM events to session replays, you need to pass the session ID from your frontend to your backend, then include it in your LLM tracking.
Frontend: Get the session ID
In your frontend code, retrieve the current session ID and send it with your API requests:
// In your frontend codeimport posthog from 'posthog-js'// Get the current session IDconst sessionId = posthog.getSessionId()// Send it with your API requestconst response = await fetch('/api/chat', {method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({message: userInput,sessionId: sessionId // Include session ID})})
Backend: Include session ID in LLM events
Once you have the session ID, include it in your LLM tracking using the $session_id
property:
app.post('/api/chat', async (req, res) => {const { message, sessionId } = req.bodyconst response = await openai.responses.create({model: 'gpt-5',messages: [{ role: 'user', content: message }],posthogDistinctId: req.userId,posthogProperties: {$session_id: sessionId, // Links to session replayendpoint: '/api/chat',request_id: generateRequestId()}})res.json({ response: response.choices[0].message.content })})
Viewing linked replays
Once you've set up session linking, you can navigate from LLM generations to their corresponding session replays:
- In the LLM analytics dashboard, find the generation or trace you're interested in
- Click the session replay button to jump directly to the replay
- Watch the user's interaction with your AI features in context
This linking helps you understand not just what your AI is doing, but how users are experiencing it in your application.