docs/agentic-tools/ai-sdk/agents/context7-agent.mdx
The Context7Agent class is a pre-configured AI agent that handles the complete documentation lookup workflow automatically. It combines both resolveLibraryId and queryDocs tools with an optimized system prompt.
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 { text } = await agent.generate({
prompt: "How do I use React Server Components?",
});
console.log(text);
new Context7Agent(config?: Context7AgentConfig)
Examples:
- `anthropic('claude-sonnet-4-20250514')`
- `openai('gpt-5.2')`
- `google('gemini-1.5-pro')`
</ParamField>
<ParamField path="apiKey" type="string" optional>
Context7 API key. If not provided, uses the `CONTEXT7_API_KEY` environment variable.
</ParamField>
<ParamField path="system" type="string" optional>
Custom system prompt. Overrides the default `AGENT_PROMPT`.
</ParamField>
<ParamField path="stopWhen" type="StopCondition" optional default="stepCountIs(5)">
Condition for when the agent should stop. Defaults to stopping after 5 steps.
</ParamField>
Context7Agent extends the AI SDK Agent class and provides generate() and stream() methods.
The agent follows a structured multi-step workflow:
flowchart TD
A[User Query] --> B[Extract Library Name]
B --> C[Call resolveLibraryId]
C --> D{Results Found?}
D -->|Yes| E[Select Best Match]
D -->|No| F[Report No Results]
E --> G[Call queryDocs]
G --> H{Sufficient Context?}
H -->|Yes| I[Generate Response]
H -->|No| J[Fetch More Docs]
J --> H
I --> K[Return Answer with Examples]
resolveLibraryId to find the Context7 library IDqueryDocs with the selected library ID and user's queryimport { Context7Agent } from "@upstash/context7-tools-ai-sdk";
import { anthropic } from "@ai-sdk/anthropic";
const agent = new Context7Agent({
model: anthropic("claude-sonnet-4-20250514"),
});
const { text } = await agent.generate({
prompt: "How do I set up authentication in Next.js?",
});
console.log(text);
import { Context7Agent } from "@upstash/context7-tools-ai-sdk";
import { openai } from "@ai-sdk/openai";
const agent = new Context7Agent({
model: openai("gpt-5.2"),
});
const { text } = await agent.generate({
prompt: "Explain Tanstack Query's useQuery hook",
});
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 { textStream } = await agent.stream({
prompt: "How do I create a Supabase Edge Function?",
});
for await (const chunk of textStream) {
process.stdout.write(chunk);
}
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"),
apiKey: process.env.CONTEXT7_API_KEY,
stopWhen: stepCountIs(8), // Allow more steps for complex queries
});
import { Context7Agent, AGENT_PROMPT } from "@upstash/context7-tools-ai-sdk";
import { openai } from "@ai-sdk/openai";
const agent = new Context7Agent({
model: openai("gpt-5.2"),
system: `${AGENT_PROMPT}
Additional instructions:
- Always include TypeScript examples
- Mention version compatibility when relevant
- Suggest related documentation topics`,
});
| Feature | Context7Agent | Individual Tools |
|---|---|---|
| Setup | Single configuration | Configure each tool |
| Workflow | Automatic multi-step | Manual orchestration |
| System prompt | Optimized default | You provide |
| Customization | Limited | Full control |
| Best for | Quick integration | Custom workflows |