The Context7Agent is a pre-configured Vercel AI SDK agent that automatically handles the multi-step workflow for documentation lookup.It extends ToolLoopAgent and comes pre-configured with:
resolveLibraryId and queryDocs tools
Optimized system prompt for documentation retrieval
import { Context7Agent } from '@upstash/context7-tools-ai-sdk';import { anthropic } from '@ai-sdk/anthropic';const agent = new Context7Agent({ model: anthropic('claude-sonnet-4-20250514'),});const result = await agent.generate({ prompt: 'How do I use React Server Components?',});console.log(result.text);
import { Context7Agent } from '@upstash/context7-tools-ai-sdk';import { openai } from '@ai-sdk/openai';const agent = new Context7Agent({ model: openai('gpt-4o'), apiKey: 'ctx7sk-...',});const result = await agent.generate({ prompt: 'How do I set up routing in Next.js?',});console.log(result.text);
import { Context7Agent } from '@upstash/context7-tools-ai-sdk';import { openai } from '@ai-sdk/openai';const customInstructions = `You are a specialized Next.js documentation assistant.Always:1. Search for the latest Next.js documentation2. Provide App Router examples when applicable3. Include TypeScript code samples4. Cite the documentation version used`;const agent = new Context7Agent({ model: openai('gpt-4o'), instructions: customInstructions,});const result = await agent.generate({ prompt: 'How do I create API routes?',});
import { Context7Agent } from '@upstash/context7-tools-ai-sdk';import { anthropic } from '@ai-sdk/anthropic';import { tool } from 'ai';import { z } from 'zod';const searchWebTool = tool({ description: 'Search the web for additional context', inputSchema: z.object({ query: z.string().describe('Search query'), }), execute: async ({ query }) => { // Your implementation return { results: [] }; },});const agent = new Context7Agent({ model: anthropic('claude-sonnet-4-20250514'), tools: { searchWeb: searchWebTool, },});// Agent now has access to: resolveLibraryId, queryDocs, and searchWebconst result = await agent.generate({ prompt: 'Find React docs and latest community discussions',});
import { Context7Agent } from '@upstash/context7-tools-ai-sdk';import { anthropic } from '@ai-sdk/anthropic';const agent = new Context7Agent({ model: anthropic('claude-sonnet-4-20250514'),});const result = await agent.stream({ prompt: 'How do I use Vue composables?',});for await (const part of result.fullStream) { switch (part.type) { case 'text-delta': process.stdout.write(part.textDelta); break; case 'tool-call': console.log('Calling tool:', part.toolName); break; case 'tool-result': console.log('Tool result received'); break; }}
You are a documentation search assistant powered by Context7.CRITICAL WORKFLOW - YOU MUST FOLLOW THESE STEPS:Step 1: ALWAYS start by calling 'resolveLibraryId' with the library name from the user's query - Extract the main library/framework name (e.g., "React", "Next.js", "Vue") - Call resolveLibraryId with just the library name - Review ALL the search results returnedStep 2: Analyze the results from resolveLibraryId and select the BEST library ID based on: - Official sources (e.g., /reactjs/react.dev for React, /vercel/next.js for Next.js) - Name similarity to what the user is looking for - Description relevance - Source reputation (High/Medium is better) - Code snippet coverage (higher is better) - Benchmark score (higher is better)Step 3: Call 'queryDocs' with the selected library ID and the user's query - Use the exact library ID from the resolveLibraryId results - Include the user's original question as the query parameterStep 4: Provide a clear answer with code examples from the documentationIMPORTANT:- You MUST call resolveLibraryId first before calling queryDocs- Do NOT skip resolveLibraryId - it helps you find the correct official documentation- Do not call either tool more than 3 times per question- Always cite which library ID you used
import { Context7Agent } from '@upstash/context7-tools-ai-sdk';import { anthropic } from '@ai-sdk/anthropic';import { stepCountIs } from 'ai';const agent = new Context7Agent({ model: anthropic('claude-sonnet-4-20250514'), stopWhen: stepCountIs(5),});const result = await agent.generate({ prompt: 'How do I use React hooks?',});// Inspect all stepsfor (const [index, step] of result.steps.entries()) { console.log(`Step ${index + 1}:`); console.log('Tool calls:', step.toolCalls.map(tc => tc.toolName)); console.log('Text:', step.text);}// Get all tool calls across all stepsconst allToolCalls = result.steps.flatMap(step => step.toolCalls);console.log('Total tools called:', allToolCalls.length);
import { Context7Agent } from '@upstash/context7-tools-ai-sdk';import { openai } from '@ai-sdk/openai';// Agent handles the workflow with optimized promptconst agent = new Context7Agent({ model: openai('gpt-4o'),});const { text } = await agent.generate({ prompt: 'How do I use React hooks?',});
Use the agent when:
You want optimized documentation retrieval out-of-the-box
You need consistent behavior across multiple queries
You want to extend with additional tools while keeping Context7 workflow
Use tools directly when:
You need full control over the system prompt
You’re integrating into an existing agent architecture
import type { Context7AgentConfig } from '@upstash/context7-tools-ai-sdk';// Use for type-safe configurationconst config: Context7AgentConfig = { model: openai('gpt-4o'), apiKey: 'ctx7sk-...', stopWhen: stepCountIs(5),};const agent = new Context7Agent(config);