Overview
Context helpers let you inject dynamic information into every AI conversation. The AI receives this context automatically and uses it to provide more relevant, personalized responses. Common use cases:- Current user information (name, role, preferences)
- Application state (selected items, current page, filters)
- Real-time data (current time, user location)
- Available actions (interactable components on screen)
How It Works
Context helpers are functions that run before each AI interaction. They return data that gets included in the conversation context. The AI can then use this information to make better decisions.import { ContextHelpers } from '@tambo-ai/react';
export const contextHelpers: ContextHelpers = {
// Return null to skip including this context
currentUser: () => {
const user = getCurrentUser();
if (!user) return null;
return {
id: user.id,
name: user.name,
role: user.role,
preferences: user.preferences,
};
},
selectedItems: () => {
const selected = getSelectedItems();
if (selected.length === 0) return null;
return {
count: selected.length,
items: selected.map(item => ({
id: item.id,
name: item.name,
type: item.type,
})),
};
},
};
import { TamboProvider } from '@tambo-ai/react';
import { contextHelpers } from '@/lib/context-helpers';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<TamboProvider
apiKey={process.env.NEXT_PUBLIC_TAMBO_API_KEY!}
userKey="user-123"
contextHelpers={contextHelpers}
>
{children}
</TamboProvider>
</body>
</html>
);
}
Built-in Context Helpers
Tambo provides pre-built context helpers for common scenarios:Current Page
Provides information about the current page:Current Time
Provides the current timestamp:Dynamic Context Helpers
Context helpers can access React state and props:components/chat-interface.tsx
Async Context Helpers
Context helpers can be async and fetch data:Conditional Context
Returnnull or undefined to skip including context:
Programmatic Context Management
Use theuseTamboContextHelpers hook to add/remove context helpers dynamically:
Best Practices
Keep context data concise
Keep context data concise
Only include information the AI needs. Excessive context can:Good:
- Increase token usage and costs
- Slow down AI response time
- Make the AI less focused
Use descriptive keys
Use descriptive keys
Context helper names become field names in the AI context. Make them clear:
Return null for empty state
Return null for empty state
Don’t send empty arrays or objects. Return null instead:
Handle errors gracefully
Handle errors gracefully
If a context helper fails, return null rather than throwing:
Context vs Tools
Context helpers provide passive information:- Current state
- Available data
- User preferences
- Application context
- Fetch data
- Update state
- Call APIs
- Execute operations
Troubleshooting
Context not appearing in AI responses
Context not appearing in AI responses
- Verify helpers are passed to
TamboProvider - Check that helpers return non-null values
- Ensure helper functions don’t throw errors
- Test helper return values by logging them
AI responses are slower after adding context
AI responses are slower after adding context
- Reduce the amount of data in each helper
- Remove context helpers that aren’t useful
- Make async operations faster or remove them
- Consider caching expensive computations
Context helpers not updating
Context helpers not updating
- If using
useMemo, verify dependencies are correct - Check that stateful data is properly synchronized
- Ensure async helpers are completing before AI interaction
Next Steps
Register Components
Define components for the AI to render
Register Tools
Add functions the AI can call
User Authentication
Secure AI access with user tokens
Additional Context Reference
Full context helper documentation