Repository navigation
Description
Summary
Add a new document_retrieve agent tool that allows the AI agent (and workflow LLM nodes) to fetch the full or partial text content of a specific document. This complements rag_search (which returns relevance-ranked chunks) by providing sequential, structure-preserving document access.
Motivation
The existing rag_search tool returns top-k chunks ranked by relevance — ideal for Q&A but insufficient when the agent needs to:
- Read a complete document to understand its structure
- Process a document section by section (e.g., clause-by-clause analysis)
- Use a full document as a template for generating new content
Large documents (50-100+ pages) easily exceed the LLM context window (~81K usable tokens ≈ ~324K chars). Paginated retrieval solves this by letting the agent request specific portions.
Proposed Tool Design
Tool name: document_retrieve
Registration: Add to agent_tools/tool_names.ts and agent_tools/tool_registry.ts
Input Schema
{
documentId: string; // Required: Convex document ID
pageRange?: string; // Optional: e.g., "1-10", "20-30" (defaults to first ~50K chars)
}Output
{
documentId: string;
name: string;
content: string; // Document text for the requested range
pageRange: { start: number; end: number };
totalPages: number;
truncated: boolean; // True if content was capped
totalChars: number;
}Implementation Notes
- Calls the RAG service endpoint from feat(rag): Add full document content retrieval endpoint #657 (
GET /api/v1/documents/{doc_id}/content) - Cap output at ~50K chars to stay within context budget (tool results are further truncated to 8K chars in history, but the current turn sees the full result)
- Resolve the document's RAG ID from Convex
documentstable usingragInfo - Pass
team_idfrom tool context for access control
Key Files
services/platform/convex/agent_tools/documents/document_retrieve_tool.ts(new)services/platform/convex/agent_tools/tool_names.ts(register name)services/platform/convex/agent_tools/tool_registry.ts(register tool)services/platform/convex/documents/(existing document queries)
Workflow Integration Pattern
For large documents, use the workflow loop node to process in batches:
Step 1 (llm): document_retrieve(docId, pageRange="1-5") → get structure/TOC
Step 2 (loop): iterate over page ranges
Each iteration (llm): document_retrieve(docId, pageRange="X-Y") → analyze section → JSON
Step 3 (llm): aggregate all iteration results → final output
Dependencies
- Depends on feat(rag): Add full document content retrieval endpoint #657 (RAG service full document content endpoint)
Acceptance Criteria
- New
document_retrievetool registered in tool registry - Supports
pageRangeparameter for paginated retrieval - Response capped at ~50K chars with
truncatedflag - Respects organization and team-based access control
- Works both as a chat agent tool and in workflow LLM nodes
- Unit tests covering: full retrieval, page ranges, truncation, access control, invalid document ID
Activity
feat(platform): add document_retrieve agent tool (#659)
yannickmonney commentedon Apr 7, 2026
This feature is already implemented as
document_retrievetool atconvex/agent_tools/documents/document_retrieve_tool.ts. It supports fileId lookup with chunk-based pagination. Tests exist at__tests__/document_retrieve_tool.test.ts(22 tests).