Skip to main content

Context Injection

Enhance your AI agent’s awareness with automatic contextual information
Context injection is a powerful system that automatically provides relevant information to your AI agent during conversations. This includes page metadata, user preferences, memories from previous interactions, and user click events—all injected seamlessly into the conversation flow.
Context injection is enabled by default to provide the best user experience. You can configure or disable specific features based on your needs.

Why Context Injection?

Traditional AI agents operate without awareness of their environment. Context injection solves this by:

Environmental Awareness

Automatically tracks page navigation and provides URL, title, and domain context

Memory Retrieval

Searches and injects relevant memories from previous interactions

User Interaction Tracking

Monitors and summarizes click events for proactive guidance

Quick Start

Context injection works out of the box with sensible defaults:

Configuration

The contextInjection prop accepts either a boolean for simple on/off control or an object for granular configuration:

Boolean Configuration

The simplest way to control context injection:

Object Configuration

For fine-grained control over individual features:
boolean
default:"true"
required
Master switch that controls whether any context injection occurs. When false, all other settings are ignored.
Enables searching and injecting relevant memories from training data and previous interactions.
boolean
default:"true"
Tracks current page URL, title, domain, and path. Updates automatically on navigation.
boolean
default:"true"
Monitors user click events and injects summaries for proactive guidance.

Features in Detail

Memory Search & Injection

When enabled, the system automatically searches for relevant memories after user turns and injects them before the agent responds.
1

User Input Detection

System detects when user completes their turn (minimum 20 characters)
2

Memory Search

Searches memory backend for relevant context based on user input
3

Context Injection

Top 3 most relevant memories are formatted and injected into the conversation
4

Agent Response

Agent responds with awareness of the injected memories
Configuration:
Memory injection only occurs after user turns to prevent infinite loops. The system tracks injected memories to avoid duplicates.

Page Context Tracking

Automatically tracks and injects page navigation context:
Page Context Structure
The system injects this information:
  • Immediately when navigation occurs (URL changes)
  • Formatted as a navigation event similar to click events
  • Tracked in observability events
Navigation events are now injected as [NAVIGATION_EVENT] messages when the URL changes, providing a more consistent experience with click events. The agent receives contextual information about the new page to provide appropriate assistance.
Configuration:

Click Event Tracking

Monitors and aggregates user click events to provide interaction context:
  1. Detection: Captures click events on truly interactive elements (buttons, links, forms, etc.)
  2. Filtering: By default, only processes clicks on interactable elements
  3. Aggregation: Groups related clicks within 500ms windows
  4. Summarization: Creates human-readable summaries
  5. Smart Timing: Injects at optimal moments based on conversation state
Configuration:
Advanced Click Detection Configuration: You can pass additional configuration to fine-tune click detection behavior:
The interactableOnly setting ensures only meaningful clicks are tracked. It filters clicks to elements like buttons, links, inputs, and elements with click handlers or ARIA roles. This reduces noise and improves the quality of context provided to the AI.
What Elements Are Considered Interactable? When interactableOnly is true (default), the system tracks clicks on:

HTML Elements

  • <a>, <button>, <input>
  • <select>, <textarea>, <label>
  • <details>, <summary>, <menu>
  • <embed>, <object>, <menuitem>

ARIA Roles

  • button, link, checkbox, radio
  • tab, menuitem, option, switch
  • slider, textbox, combobox
  • progressbar, scrollbar, tree
Additional criteria for interactable elements:
  • Elements with tabindex (except -1)
  • Elements with click handlers (onclick, @click, ng-click, etc.)
  • Elements with ARIA properties (aria-expanded, aria-pressed, etc.)
  • Draggable elements (draggable="true")
  • Content-editable elements (contenteditable)
The system also traverses up the DOM tree to find interactable parent elements, so clicks on button text or icons are properly captured.

Use Cases

Customer Support Agent

Full context awareness for comprehensive support:
Benefits:
  • Access to product knowledge through memories
  • Awareness of user’s current page
  • Understanding of user interactions

Documentation Helper

Page context only for documentation navigation:
Benefits:
  • Knows which documentation page user is viewing
  • Can provide page-specific guidance
  • Lightweight without unnecessary features

Interactive Tutorial

Click and page tracking for guided experiences:
Benefits:
  • Tracks tutorial progression
  • Responds to user interactions
  • Provides contextual hints

Debugging User Interactions

For troubleshooting UI issues, you might want to track ALL clicks:
Benefits:
  • Captures all user interactions for debugging
  • Helps identify dead zones or broken elements
  • Provides complete interaction telemetry

Simple Q&A Bot

Minimal configuration for basic interactions:
Benefits:
  • Reduced latency
  • Lower resource usage
  • Simpler conversation flow

Programmatic Access

You can interact with the context system programmatically:

Checking Context Status

Manual Context Injection

Even with automatic injection enabled, you can inject custom context:

React Hook Integration

Use the useContextUpdater hook for automatic page tracking:

Performance Considerations

When to Disable

Consider disabling context injection for:
Performance-Critical Applications
  • Each injection adds processing overhead
  • Memory searches require API calls
  • Consider disabling for high-frequency interactions
Privacy-Sensitive Environments
  • No data collection when disabled
  • URLs and interactions not tracked
  • Memories not searched or stored
Simple Use Cases
  • Basic chatbots without context needs
  • Static help systems
  • Single-purpose tools

Impact Analysis

When context injection is disabled:

Advanced Configuration

Memory Search Settings

The memory search system has internal configuration:
Memory Search Defaults

Context Formatting

Context is injected using XML-style tags for clarity:
Example Injected Context

Debugging

Console Logs

Monitor context injection activity in the console:

Observability Events

All context operations are tracked for monitoring:

Migration Guide

From Previous Versions

If upgrading from a version where context was always enabled:
1

Review Current Usage

Identify which context features your application uses
2

Update Configuration

3

Test and Optimize

Gradually disable unused features:

Best Practices

Troubleshooting

Common Issues

Symptoms: Agent seems unaware of contextCheck:
  • Verify contextInjection is not set to false
  • Ensure specific features are enabled
  • Check console for injection logs
  • Verify connection status: contextInjector?.isReady()
Solution:

API Reference

Configuration Types

Context Manager Methods

Summary

Context injection provides intelligent, automatic context management that enhances your AI agent’s responses. With flexible configuration options, you can optimize for your specific use case—whether you need full contextual awareness for complex interactions or a lightweight setup for simple queries.
Remember: Context injection is enabled by default because it significantly improves the quality of AI responses. Only disable features that you’re certain you don’t need.