|
| 1 | +# ADR-0006: Context Injection Pattern for Conversational AI |
| 2 | + |
| 3 | +> **Scope**: Pattern for injecting user profile, page context, and conversation history into conversational AI agent prompts. Includes client-side context extraction, request metadata transmission, backend extraction, and prompt construction. This pattern affects how agents understand user context and provide personalized responses. |
| 4 | +
|
| 5 | +- **Status:** Accepted |
| 6 | +- **Date:** 2025-01-27 |
| 7 | +- **Feature:** 007-chatkit-server, 008-chatkit-ui-widget |
| 8 | +- **Context:** Conversational AI agents need context to provide personalized, relevant responses. Three types of context are available: (1) user profile (name, role, hardware tier, preferences), (2) page context (current page URL, title, headings, description), and (3) conversation history (previous messages). Multiple approaches considered: (1) pass context as separate messages, (2) include in system prompt, (3) use agent memory/state. System prompt inclusion chosen (CarFixer pattern) for simplicity and reliability. Context must be extracted client-side (page info), transmitted via request metadata, extracted in backend, and included in agent prompt. |
| 9 | + |
| 10 | +<!-- Significance checklist (ALL must be true to justify this ADR) |
| 11 | + 1) Impact: ✅ Defines how agents receive context, affects personalization and relevance of all responses |
| 12 | + 2) Alternatives: ✅ Separate messages vs system prompt, client-side vs server-side extraction, different transmission methods |
| 13 | + 3) Scope: ✅ Cross-cutting pattern affecting frontend, backend, and agent integration |
| 14 | +--> |
| 15 | + |
| 16 | +## Decision |
| 17 | + |
| 18 | +Use **Context Injection via System Prompt** pattern with the following components: |
| 19 | + |
| 20 | +- **History Pattern**: Include conversation history as formatted string in system prompt (CarFixer pattern), load last 10 messages from thread |
| 21 | +- **User Context**: Extract user profile from authenticated session, include in request metadata, extract in backend, format as string in prompt |
| 22 | +- **Page Context**: Extract page metadata client-side (URL, title, headings, meta tags), include in request metadata, extract in backend, format as string in prompt |
| 23 | +- **Prompt Construction**: Combine history + user context + page context + system prompt into single prompt string |
| 24 | +- **Transmission Method**: Request metadata (JSON in ChatKit protocol body) for user/page context, database query for history |
| 25 | + |
| 26 | +## Consequences |
| 27 | + |
| 28 | +### Positive |
| 29 | + |
| 30 | +- **Simplicity**: Single prompt with all context, no complex state management |
| 31 | +- **Proven Pattern**: Matches CarFixer implementation, works reliably |
| 32 | +- **Agent SDK Compatibility**: Works with current OpenAI Agents SDK version |
| 33 | +- **Client-Side Rich Context**: Can extract detailed page metadata (headings, meta tags) that server can't access |
| 34 | +- **No Extra Requests**: Context included in same request, no additional round-trips |
| 35 | +- **Framework Agnostic**: Pattern works with any agent framework that accepts system prompts |
| 36 | + |
| 37 | +### Negative |
| 38 | + |
| 39 | +- **Prompt Length Growth**: Prompt length increases with history (mitigated by limit=10 messages) |
| 40 | +- **String Formatting**: Must format context as strings, less structured than separate parameters |
| 41 | +- **Client Dependency**: Page context only available if client sends it (could be spoofed) |
| 42 | +- **No Incremental Updates**: Full context rebuilt on each request (not incremental) |
| 43 | +- **Token Usage**: Context included in every request, increases token usage |
| 44 | + |
| 45 | +## Alternatives Considered |
| 46 | + |
| 47 | +**Alternative Pattern A: Pass Context as Separate Messages** |
| 48 | +- Include user/page context as separate user messages in conversation |
| 49 | +- **Why rejected**: Pollutes conversation history, context appears as user messages, less clean |
| 50 | + |
| 51 | +**Alternative Pattern B: Agent Memory/State** |
| 52 | +- Use agent SDK's built-in memory/state management for context |
| 53 | +- **Why rejected**: Not available in current SDK version, would require SDK updates |
| 54 | + |
| 55 | +**Alternative Pattern C: Server-Side Page Context Detection** |
| 56 | +- Detect page context server-side using URL or referrer header |
| 57 | +- **Why rejected**: Server can't access DOM (headings, meta tags), less rich context |
| 58 | + |
| 59 | +**Alternative Pattern D: Context in Query Parameters** |
| 60 | +- Pass context via URL query parameters instead of request body |
| 61 | +- **Why rejected**: Exposes context in URLs/logs, less secure, doesn't work with ChatKit protocol |
| 62 | + |
| 63 | +**Alternative Pattern E: Incremental Context Updates** |
| 64 | +- Only send changed context, maintain context state server-side |
| 65 | +- **Why rejected**: More complex state management, requires context diffing, not needed for current use case |
| 66 | + |
| 67 | +## References |
| 68 | + |
| 69 | +- Feature Spec: `specs/007-chatkit-server/spec.md` (Requirement 5: User Context Injection, Requirement 7: Conversation History Management) |
| 70 | +- Feature Spec: `specs/008-chatkit-ui-widget/spec.md` (Requirement 4: Page Context Extraction, Requirement 6: User Profile Context Transmission) |
| 71 | +- Implementation Plan: `specs/007-chatkit-server/plan.md` (Pattern 2: Context Injection, Pattern 3: History in Prompt) |
| 72 | +- Related ADRs: ADR-0005 (ChatKit Framework Integration Architecture) |
| 73 | +- Evaluator Evidence: `history/prompts/007-chatkit-server/0001-chatkit-server-reverse-engineering.spec.prompt.md`, `history/prompts/008-chatkit-ui-widget/0001-chatkit-ui-widget-reverse-engineering.spec.prompt.md` |
0 commit comments