Skip to main content
Context tells the agent what’s happening in your app right now. Without it, the agent doesn’t know what’s on screen, what items exist, or what the user is looking at.

Why Context Matters

User: “Delete the first one”Assistant: ❌ “I don’t know what items you’re looking at. Could you specify which item to delete?”
Without context, the assistant doesn’t know what’s on screen. With context, it can act immediately.

The getContext Function

Pass a getContext function that returns the current app state:
const assistant = useAssistant({
    availableActions: [...],
    getContext: () => ({
        currentPage: "/dashboard",
        selectedProject: currentProject,
        visibleItems: items.map(i => ({ id: i.id, title: i.title })),
        filters: { status: "active", sort: "date" },
        userPreferences: { theme: "dark" },
    }),
    onAction: handleAction,
})

What to Include

{
    currentPage: "/projects/123",
    currentRoute: "project-detail",
    breadcrumbs: ["Home", "Projects", "Marketing"],
}
{
    visibleProjects: projects.map(p => ({
        id: p.id,
        name: p.name,
        status: p.status,
    })),
    selectedItem: selectedProject ? {
        id: selectedProject.id,
        name: selectedProject.name,
    } : null,
}
{
    isModalOpen: showModal,
    modalType: modalType,
    sidebarCollapsed: isSidebarCollapsed,
    activeTab: currentTab,
}
{
    userRole: user.role,
    permissions: user.permissions,
    preferences: user.settings,
}

Keep Context Lean

Don’t dump your entire app state. Include only what’s relevant for the assistant to understand and act.
getContext: () => ({
    currentPage: "projects",
    visibleProjects: projects.slice(0, 10).map(p => ({
        id: p.id,
        name: p.name,
    })),
    selectedId: selected?.id,
})

Dynamic Context

The getContext function is called fresh on each message. It should always return the current state:
getContext: () => ({
    // This updates as your state changes
    items: items,
    selectedCount: selectedItems.length,
    canDelete: selectedItems.length > 0,
})

Context for Dynamic UI

If you’re using Dynamic UI, include the current buttons so the AI knows what shortcuts exist:
const dynamicUI = useDynamicUI({ storageKey: "my-app-buttons" })

getContext: () => ({
    quickActionButtons: dynamicUI.tree
        ? Object.values(dynamicUI.tree.elements)
            .filter(el => el.type === "DynamicButton")
            .map(el => ({
                id: el.props.id,
                label: el.props.label,
            }))
        : [],
    // ... rest of context
})

Next Steps

Dynamic UI

Let the assistant create persistent action shortcuts