AutoRagAgent
AutoRagAgent is an agent wrapper that automatically decides when to retrieve context from knowledge bases versus direct chat, based on query heuristics.
Overview
AutoRagAgent wraps an existingAgent and adds intelligent retrieval decision-making:
- auto policy: Decides based on query keywords, length, and question marks
- always policy: Always retrieves context before responding
- never policy: Never retrieves, direct chat only
When to Use
| Use Case | Recommended |
|---|---|
| Agent with knowledge that should auto-retrieve | ✅ AutoRagAgent |
| Simple chat without knowledge | ❌ Use Agent directly |
| Always need RAG context | ✅ AutoRagAgent with always policy |
| Fine-grained retrieval control | ❌ Use RAG.retrieve() + Agent.chat_with_context() |
Installation
Quick Start
API Reference
AutoRagAgent
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
agent | Agent | required | Agent instance with knowledge configured |
rag | RAG | None | Optional RAG instance (uses agent.rag if not provided) |
config | AutoRagConfig | None | Full configuration object |
retrieval_policy | str | "auto" | When to retrieve: auto, always, never |
top_k | int | 5 | Number of results to retrieve |
hybrid | bool | False | Enable hybrid retrieval (dense + BM25) |
rerank | bool | False | Enable reranking of results |
citations | bool | True | Include citations in response |
chat()
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
message | str | required | User message/query |
force_retrieval | bool | False | Force retrieval regardless of policy |
skip_retrieval | bool | False | Skip retrieval regardless of policy |
top_k | int | None | Override top_k for this call |
hybrid | bool | None | Override hybrid setting for this call |
rerank | bool | None | Override rerank setting for this call |
user_id | str | None | User ID for RAG (uses agent.user_id if not provided) |
RetrievalPolicy
AutoRagConfig
Examples
Basic Usage
Force/Skip Retrieval
Different Policies
With Hybrid Retrieval and Reranking
Async Usage
CLI Usage
Decision Heuristics
AutoRagAgent uses simple, local heuristics (no ML classifier) to decide when to retrieve:- Minimum length: Queries shorter than 10 characters skip retrieval
- Keywords: Queries containing keywords like “what”, “how”, “why”, “explain”, “find”, “search” trigger retrieval
- Question marks: Queries ending with ”?” trigger retrieval
Performance
- Import overhead: ~1.6ms (lazy loaded)
- Decision overhead: less than 1ms (local heuristics only)
- No ML classifier: Zero additional dependencies
Comparison
| Feature | Agent | RAG | AutoRagAgent |
|---|---|---|---|
| Direct chat | ✅ | ❌ | ✅ |
| Knowledge retrieval | Manual | Always | Auto-decides |
| Citations | Manual | ✅ | ✅ |
| Hybrid retrieval | Manual | ✅ | ✅ |
| Reranking | Manual | ✅ | ✅ |
| Policy control | ❌ | ❌ | ✅ |

