Knowledge Base & Context Sources¶
This guide explains how to provide agents with context and knowledge beyond their base system prompt.
Overview¶
Ag2Trust provides multiple ways to inject context into your agents:
| Source | Description | When Resolved |
|---|---|---|
| Built-in Variables | Platform-provided values (date, time, agent name) | Per request |
| Custom Variables | Customer-defined key-value pairs | Per request |
| Runtime Variables | Values passed with each API request | Per request |
| Documents | Uploaded files (PDF, DOCX, etc.) | Per request |
All context is assembled by the backend per-request and injected into the agent's effective system prompt. This keeps agents stateless and allows KB changes to take effect immediately.
Context Scoping¶
Resources can be attached to different scopes, allowing inheritance and override:
┌─────────────────────────────────────────────────┐
│ Organization (Customer-wide) │ ← Lowest precedence
│ └── Team │
│ └── Agent Type │ ← Highest precedence
└─────────────────────────────────────────────────┘
Precedence rules (later overrides earlier):
- Organization - Applies to all agents in your organization
- Team - Applies to all agents in a specific team
- Agent Type - Applies to agents of a specific type
For variables, values from higher-precedence scopes override lower-precedence ones. For documents, all attached documents from applicable scopes are included.
Built-in Variables¶
Platform-provided variables that are resolved at request time. These are opt-in per agent type.
Available Variables¶
| Variable | Example | Description |
|---|---|---|
current_date | December 26, 2025 | Today's date (human readable) |
current_date_iso | 2025-12-26 | Today's date (ISO format) |
current_time | 2:30 PM EST | Current time with timezone |
current_datetime | 2025-12-26T14:30:00-05:00 | Full ISO datetime |
current_year | 2025 | Current year |
day_of_week | Thursday | Current day name |
agent_name | Support Bot | Agent type display name |
agent_type | customer-support | Agent type slug |
customer_name | Acme Corp | Organization name |
conversation_id | conv_abc123 | Current thread ID |
Enabling Built-in Variables¶
- Go to Agent Types and select an agent type
- Navigate to Context Settings
- Check the variables you want to enable
- Click Save
How They Appear in Context¶
Enabled variables are injected as a structured block:
## Context Variables
- current_date: December 26, 2025
- current_year: 2025
- agent_name: Support Bot
Token Efficiency
Only enable variables your agent actually needs. Each variable adds to the context size and token usage.
Custom Variables¶
Customer-defined variables for company-specific information. Each variable is an individual record with a name, data type, and optional validation.
Use Cases¶
- Company contact information
- Product names and versions
- Current promotions or announcements
- Support hours and policies
- Feature flags or configuration values
Creating Variables¶
- Go to Knowledge Base > Variables
- Click Create Variable
- Configure the variable:
- Name:
UPPERCASE_SNAKE_CASEidentifier (e.g.,SUPPORT_EMAIL) - Label: Human-readable display name
- Data type:
string,int,float,boolean, ordate - Validator:
none,email,url,phone, orregex - Static vs Dynamic: Static variables store an encrypted value; dynamic variables are provided at runtime via
context_variables - Click Save
Variable Properties¶
| Property | Description |
|---|---|
name | UPPERCASE_SNAKE_CASE, max 100 chars |
data_type | string (default), int, float, boolean, date |
validator | none (default), email, url, phone, regex |
validator_pattern | Regex pattern (when validator is regex) |
is_dynamic | true = provided at runtime; false = stored encrypted value |
visible_to_agent | true = inject value; false = placeholder only |
static_value | Stored value (KMS-encrypted) when is_dynamic is false |
default_value | Fallback for dynamic variables if not provided at runtime |
Reserved Variable Names¶
The following names are reserved for built-in variables and cannot be used:
CURRENT_DATE, CURRENT_TIME, CURRENT_DATETIME, AGENT_NAME, AGENT_TYPE, CUSTOMER_NAME, TEAM_NAME, CONVERSATION_ID, THREAD_ID, SESSION_ID, REQUEST_ID
Attaching Variables to Scopes¶
After creating variables, attach them to scopes in the dashboard:
- Go to Knowledge Base > Context Attachments
- Click Attach Resource
- Select the variable and the desired scope:
| Scope | Description |
|---|---|
| Organization | Applies to all agents in your organization |
| Team | Applies to all agents in a specific team |
| Agent Type | Applies to agents of a specific type |
How They Appear in Context¶
Custom variables are injected as a structured block:
## Custom Variables
- PRODUCT_NAME: AcmeWidget Pro
- SUPPORT_EMAIL: support@acme.com
- SUPPORT_HOURS: Monday-Friday, 9am-6pm EST
Runtime Variables¶
Variables passed with each API request, allowing dynamic per-request context.
Passing Runtime Variables¶
Include context_variables in your API request:
POST /api/v1/ask/support
{
"content": "What's my order status?",
"context_variables": {
"USER_NAME": "John Smith",
"USER_EMAIL": "john@example.com",
"USER_PLAN": "premium",
"PENDING_ORDERS": "2"
}
}
Constraints¶
| Limit | Value |
|---|---|
| Maximum variables | 20 per request |
| Maximum total size | 4KB |
| Key format | UPPERCASE_SNAKE_CASE |
| Value format | String only (no nested objects) |
How They Appear in Context¶
Runtime variables are injected as a structured block:
## User Context (provided at request time)
- user_name: John Smith
- user_email: john@example.com
- user_plan: premium
- pending_orders: 2
Not Persisted
Runtime variables are ephemeral and not stored. They only exist for the duration of the request.
Knowledge Documents¶
Upload documents to provide agents with reference material.
Supported Formats¶
| Format | Extension | Notes |
|---|---|---|
.pdf | Text extracted via Tika | |
| Word | .docx | Full text extraction |
| Excel | .xlsx | Converted to markdown tables |
| CSV | .csv | Converted to markdown tables |
| Plain Text | .txt | Direct read |
| Markdown | .md | Direct read |
| HTML | .html | Tags stripped, text extracted |
Uploading Documents¶
- Go to Knowledge Base > Documents
- Drag and drop files or click to browse
- Wait for text extraction to complete
- Attach to desired scopes
You can also upload documents programmatically via the Customer API. See the Knowledge Base API reference.
Document Processing States¶
| Status | Description |
|---|---|
pending | Queued for text extraction |
processing | Extraction in progress |
ready | Extraction complete, document available |
failed | Extraction failed (check error message) |
Only documents with status: ready are included in agent context.
Attaching Documents to Scopes¶
After uploading, attach documents to scopes in the dashboard:
- Go to Knowledge Base > Context Attachments
- Click Attach Resource
- Select the document and the desired scope (Organization, Team, or Agent Type)
- Set a priority value — higher priority documents appear earlier in the context
How Documents Appear in Context¶
Documents are injected with clear boundaries:
## Reference Documentation
Use the following documentation to answer questions accurately.
### Product Manual
<context_document doc_id="123" name="Product Manual">
[Full extracted text from the document...]
</context_document>
### FAQ Document
<context_document doc_id="124" name="FAQ Document">
[Full extracted text from the document...]
</context_document>
Source Citations¶
Enable source citations to have agents reference document sources:
- Go to Agent Types > select type > Context Settings
- Enable Include source citations
- Save changes
With citations enabled, documents include source IDs:
And agents are instructed to cite sources inline:
Upload Limits¶
| Constraint | Value |
|---|---|
| Maximum file size | 10MB per file |
| Supported formats | PDF, DOCX, XLSX, CSV, TXT, MD, HTML |
Token Budget
Large documents consume significant context tokens. Use RAG mode or monitor your token usage in the dashboard.
Live Context¶
Live context entries provide dynamic data that is resolved at agent assembly time. Unlike static documents or variables, live context can fetch fresh data from external sources on every request.
Use Cases¶
- Real-time inventory or pricing data
- Current user account status from your backend
- Live system health or metrics
- Dynamic configuration that changes frequently
Resolver Types¶
| Type | Description |
|---|---|
| builtin | Platform-provided resolvers (e.g., current date/time) |
| custom | Your own webhook endpoint that returns context data |
Creating a Live Context Entry¶
- Go to Knowledge Base > Live Context
- Click Add Live Context
- Configure:
- Slug: Unique identifier (e.g.,
inventory-status) - Display Name: Human-readable label
- Resolver Type:
builtinorcustom - For custom resolvers: provide a webhook URL
- Click Save
Available Builtin Resolvers¶
View available builtin resolvers in the dashboard under Knowledge Base > Live Context > Add Live Context > Resolver Type dropdown.
Configuration Options¶
| Field | Description | Default |
|---|---|---|
cache_ttl_seconds | How long to cache resolved data | 3600 (1 hour) |
refresh_interval_seconds | Background refresh interval (null = sync only) | null |
enabled | Whether this entry is active | true |
How It Works¶
- Agent receives a request
- Backend checks for live context entries attached to the agent's scope
- For each entry, the resolver is called (or cache is used)
- Resolved data is injected into the agent's context alongside variables and documents
Context Modes¶
Ag2Trust supports three modes for injecting knowledge into agent context:
| Mode | Behavior | Best For |
|---|---|---|
| auto (default) | Direct injection for small doc sets, switches to RAG when tokens exceed 10K | Most use cases |
| rag | Always use semantic search to retrieve relevant chunks | Large knowledge bases |
| direct | Always inject full document content | Small, critical docs that must always be included |
Configuring Context Mode¶
- Go to Agent Types > select type
- Navigate to Context Settings
- Select the desired Context Mode
- Click Save
How RAG Works¶
When using rag mode (or auto mode with large document sets):
- Documents are chunked and embedded when uploaded
- At request time, the user's message is embedded
- Most relevant chunks are retrieved via semantic similarity
- Only relevant chunks are injected into context
This significantly reduces token usage for large knowledge bases while maintaining answer quality.
Choosing a Context Mode¶
Small KB (< 10K tokens total) → auto or direct
Large KB (> 10K tokens) → auto or rag
Critical reference docs → direct (ensures always included)
FAQ / support knowledge → rag (retrieves relevant answers)
Context Assembly¶
At request time, the backend assembles the effective context:
┌─────────────────────────────────────────────────────────────────────────┐
│ REQUEST ARRIVES │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ 1. Load base system prompt from agent type │
│ 2. Resolve built-in variables (if enabled) │
│ 3. Load custom variables from org → team → agent_type scopes │
│ 4. Merge variables (later scopes override earlier) │
│ 5. Add runtime variables (highest precedence) │
│ 6. Load documents from all applicable scopes │
│ 7. Assemble final context block │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ FINAL PROMPT = Base System Prompt + Context Block │
└─────────────────────────────────────────────────────────────────────────┘
Customer API Reference¶
The following endpoints are available to API key holders for managing knowledge documents programmatically. See the Knowledge Base API reference for full details.
| Method | Endpoint | Description |
|---|---|---|
POST | /api/v1/knowledge/documents | Upload document |
GET | /api/v1/knowledge/documents | List documents |
GET | /api/v1/knowledge/documents/{id} | Get document |
DELETE | /api/v1/knowledge/documents/{id} | Delete document |
GET | /api/v1/account/usage | Account usage stats |
GET | /api/v1/account/limits | Tier limits |
Best Practices¶
1. Use Scopes Strategically¶
Organization scope:
└── Company-wide info (support email, company name)
Team scope:
└── Team-specific docs (sales playbook, engineering runbooks)
Agent Type scope:
└── Agent-specific context (specialized prompts, tools docs)
2. Keep Variables Focused¶
# Good - specific, actionable
support_email: support@acme.com
refund_policy_days: 30
# Avoid - too generic, wastes tokens
company_info: "Acme Corp was founded in 1985 and has grown..."
3. Monitor Token Usage¶
- Check token counts in the Knowledge Base dashboard
- Large documents significantly increase costs
- Consider splitting very large documents
4. Use Priority for Document Order¶
priority: 10 → Most important, appears first
priority: 0 → Default
priority: -10 → Less important, appears last
5. Test Context Assembly¶
Use the agent chat in the dashboard to verify context is being injected correctly before deploying to production.
Limits & Considerations¶
| Consideration | Details |
|---|---|
| Document size | Max 10MB per file |
| Knowledge tokens | Tier-dependent limits (100K free → 2M pro) |
| RAG chunk size | Documents chunked for semantic search |
| Embedding latency | Large docs may take time to process after upload |
Next Steps¶
- Agents - Learn about agent configuration
- Agent Types - Configure agent templates
- Teams - Organize agents into teams
- API Reference - Complete API documentation