Skip to main content
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.

Usage

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);

Configuration

new Context7Agent(config?: Context7AgentConfig)

Parameters

config
Context7AgentConfig
Configuration options for the agent.

Returns

Context7Agent extends the AI SDK Agent class and provides generate() and stream() methods.

Agent Workflow

The agent follows a structured multi-step workflow:

Step-by-Step

  1. Extract library name - Identifies the library/framework from the user’s query
  2. Resolve library - Calls resolveLibraryId to find the Context7 library ID
  3. Select best match - Analyzes results based on reputation, coverage, and relevance
  4. Fetch documentation - Calls queryDocs with the selected library ID and user’s query
  5. Query if needed - Makes additional queries if initial context is insufficient
  6. Generate response - Provides an answer with code examples from the documentation

Examples

Basic Usage

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 set up authentication in Next.js?",
});

console.log(text);

With OpenAI

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",
});

Streaming Responses

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);
}

Custom Configuration

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
});

Custom System Prompt

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`,
});

Comparison: Agent vs Tools

FeatureContext7AgentIndividual Tools
SetupSingle configurationConfigure each tool
WorkflowAutomatic multi-stepManual orchestration
System promptOptimized defaultYou provide
CustomizationLimitedFull control
Best forQuick integrationCustom workflows

When to Use the Agent

  • Rapid prototyping
  • Standard documentation lookup use cases
  • When you want sensible defaults

When to Use Individual Tools

  • Custom agentic workflows
  • Integration with other tools
  • Fine-grained control over the process
  • Custom system prompts with specific behavior