DEV Community

Cover image for Context engineering 101: Branch AI conversations with Langbase
Muhammad Mairaj
Muhammad Mairaj

Posted on

Context engineering 101: Branch AI conversations with Langbase

Long conversations with AI often go off track. Topics pile up, irrelevant details linger, and eventually responses degrade. This is what we call context rot.

The fix? Branching conversations.

Why Branch Conversations?

Branching is a form of context engineering. Instead of keeping one messy thread, you split the conversation at decision points. Each branch evolves independently, while the original conversation remains intact.

Benefits include:

  • Prevent context drift between different discussion paths
  • Reduce token usage by trimming unnecessary history
  • Explore in parallel without polluting the main thread
  • Merge insights back when you’re ready

Think of it as Git for conversations: fork, explore, and (optionally) merge.

Getting Started with Langbase

Branching with Langbase takes just a few lines of code. Let’s walk through it step by step.

Step 1: Install Dependencies

Install the Langbase SDK in your project:

npm i langbase dotenv
# or
pnpm add langbase dotenv
# or
yarn add langbase dotenv
Enter fullscreen mode Exit fullscreen mode

Step 1.1: Langbase API key

Every request you send to Langbase needs an API key. You need to generate your API key by following these steps:

  1. Sign up at Langbase.com
  2. From the sidebar, click on the API keys
  3. From here, you can create a new API key. For more details, follow this guide

Create a .env file and add your API key:

LANGBASE_API_KEY=xxxxxxxxx
Enter fullscreen mode Exit fullscreen mode

Initialize Langbase with your API key:

import dotenv from 'dotenv';
import { Langbase, ThreadMessage } from 'langbase';

dotenv.config();

const langbase = new Langbase({
  apiKey: process.env.LANGBASE_API_KEY!,
});
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Initial Conversation

Let’s start with a practical example: choosing state management for a React app.

async function createConversation() {
  const thread = await langbase.threads.create({
    messages: [
      { role: 'user', content: 'I need to add state management to my React app' },
      { role: 'assistant', content: 'How complex is your app and what are your main requirements?' },
      { role: 'user', content: "It's medium-sized, with user data, API calls, and real-time updates" },
      { role: 'assistant', content: 'You could use Redux for its ecosystem, or Zustand for simplicity. Which do you prefer?' },
    ],
  });

  return thread.id;
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Branch the Conversation

At the decision point (Redux vs Zustand), create a new branch:

async function branchThread(threadId: string, branchAt: number) {
  const messages = await langbase.threads.messages.list({ threadId });
  const messagesToKeep = messages.slice(0, branchAt);

  const branch = await langbase.threads.create({
    messages: messagesToKeep as ThreadMessage[],
    metadata: {
      parent: threadId,
      branchedAt: branchAt.toString(),
    },
  });

  return branch.id;
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Continue Each Branch

Now both threads evolve independently:

async function main() {
  const originalId = await createConversation();
  const branchId = await branchThread(originalId, 4);

  // Continue with Redux
  await langbase.threads.append({
    threadId: originalId,
    messages: [
      { role: 'user', content: "Let's go with Redux" },
      { role: 'assistant', content: 'Great choice! Redux Toolkit makes setup easier. Here’s how…' },
    ],
  });

  // Explore Zustand
  await langbase.threads.append({
    threadId: branchId,
    messages: [
      { role: 'user', content: 'Tell me about Zustand' },
      { role: 'assistant', content: "Zustand is lightweight and only 2KB. Here’s how to get started…" },
    ],
  });
}

main();
Enter fullscreen mode Exit fullscreen mode

You now have two independent discussions:

  • Original → continues with Redux
  • Branch → explores Zustand

Step 5: Run It

npx tsx index.ts
Enter fullscreen mode Exit fullscreen mode

You’ll see two clean, focused threads in your console.

Why this matters

Branching prevents conversations from collapsing under their own weight. Instead of a single, tangled thread, you get structured trees of thought:

  • Keep threads modular and adaptive
  • Reuse branches for future work
  • Merge insights or summaries back into the main conversation

With Langbase, branching isn’t just possible — it’s simple.

Top comments (0)