GuidesLangChain
Stable ID
Keep sessions and artifacts scoped to each user.
Pass a stableId to scope sessions and file storage so that one user's artifacts never leak into another
user's sandbox. Always pass a unique value that maps to your application's user ID.
Setting stableId in the constructor
import { Bluebag } from "@bluebag/langchain";
const bluebag = new Bluebag({
apiKey: process.env.BLUEBAG_API_KEY!,
stableId: user.id,
});
const config = await bluebag.enhance({ messages });Setting stableId dynamically
You can also set or update the stableId after creating the client:
import { Bluebag } from "@bluebag/langchain";
const bluebag = new Bluebag({
apiKey: process.env.BLUEBAG_API_KEY!,
});
// Set stableId before calling enhance
bluebag.setStableId(user.id);
const config = await bluebag.enhance({ messages });Stable IDs must be unique per user and should be URL-safe (letters, numbers, -, _), up to 64 characters.
Multi-tenant considerations
For multi-tenant applications, each user gets an isolated sandbox session:
import { Bluebag } from "@bluebag/langchain";
import { createAgent } from "langchain";
import { HumanMessage } from "@langchain/core/messages";
async function handleUserRequest(userId: string, message: string) {
const bluebag = new Bluebag({
apiKey: process.env.BLUEBAG_API_KEY!,
stableId: `user-${userId}`,
activeSkills: ["data-analysis", "code-review"],
});
const config = await bluebag.enhance({
model: "openai:gpt-4o",
messages: [new HumanMessage(message)],
});
const agent = createAgent({
model: config.model,
tools: config.tools,
systemPrompt: config.systemMessage,
});
return agent.invoke({ messages: config.messages });
}This ensures:
- Each user's sandbox is isolated
- Files created by one user are not accessible to others
- Session context persists across calls for the same user