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 variable sets, variables 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
- Save changes
PATCH /api/agent-types/{id}/context-settings
{
"enabled_builtin_variables": [
"current_date",
"current_year",
"agent_name"
]
}
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 Variable Sets¶
Customer-defined key-value pairs for company-specific information.
Use Cases¶
- Company contact information
- Product names and versions
- Current promotions or announcements
- Support hours and policies
- Feature flags or configuration values
Creating a Variable Set¶
Via Dashboard:
- Go to Knowledge Base > Variable Sets
- Click Create Variable Set
- Enter a name and description
- Add key-value pairs
- Click Save
Via API:
POST /api/knowledge/variable-sets
Content-Type: application/json
{
"name": "Support Defaults",
"description": "Common support information",
"variables": {
"product_name": "AcmeWidget Pro",
"support_email": "support@acme.com",
"support_hours": "Monday-Friday, 9am-6pm EST",
"refund_policy_days": "30"
}
}
Variable Naming Rules¶
- Must start with a letter
- Can contain letters, numbers, and underscores
- Maximum 64 characters per name
- Maximum 50 variables per set
- Maximum 32KB total size per set
Attaching Variable Sets to Scopes¶
After creating a variable set, attach it to a scope:
POST /api/context/attachments
{
"resource_type": "variable_set",
"resource_id": 42,
"scope_type": "team",
"scope_id": 5,
"priority": 0
}
| Scope Type | scope_id | Description |
|---|---|---|
org | null | Applies to entire organization |
team | Team ID | Applies to specific team |
agent_type | Agent Type ID | Applies to specific agent 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/agents/{id}/messages
{
"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 | 10 per request |
| Maximum total size | 1KB |
| Key format | Alphanumeric + underscore |
| 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¶
Via Dashboard:
- Go to Knowledge Base > Documents
- Drag and drop files or click to browse
- Wait for text extraction to complete
- Attach to desired scopes
Via API:
POST /api/knowledge/documents
Content-Type: multipart/form-data
file: <binary>
name: "Product Manual" # Optional display name
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¶
POST /api/context/attachments
{
"resource_type": "document",
"resource_id": 123,
"scope_type": "agent_type",
"scope_id": 7,
"priority": 10
}
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.
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¶
Via Dashboard:
- Go to Agent Types > select type
- Navigate to Context Settings
- Select the desired Context Mode
- Save changes
Via API:
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 variable sets 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 │
└─────────────────────────────────────────────────────────────────────────┘
API Reference¶
Variable Sets¶
| Method | Endpoint | Description |
|---|---|---|
GET | /api/knowledge/variable-sets | List all variable sets |
POST | /api/knowledge/variable-sets | Create variable set |
GET | /api/knowledge/variable-sets/{id} | Get variable set |
PUT | /api/knowledge/variable-sets/{id} | Update variable set |
DELETE | /api/knowledge/variable-sets/{id} | Delete variable set |
Documents¶
| Method | Endpoint | Description |
|---|---|---|
GET | /api/knowledge/documents | List all documents |
POST | /api/knowledge/documents | Upload document |
GET | /api/knowledge/documents/{id} | Get document details |
DELETE | /api/knowledge/documents/{id} | Delete document |
Context Attachments¶
| Method | Endpoint | Description |
|---|---|---|
GET | /api/context/attachments | List attachments |
POST | /api/context/attachments | Create attachment |
DELETE | /api/context/attachments/{id} | Delete attachment |
Agent Type Context Settings¶
| Method | Endpoint | Description |
|---|---|---|
GET | /api/agent-types/{id}/context-settings | Get context config |
PATCH | /api/agent-types/{id}/context-settings | Update context config |
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