Skip to main content

Overview

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
  • Sensible defaults for step count and workflow

Class Signature

Configuration

Inherited Settings from ToolLoopAgentSettings

  • model - The AI model to use (required)
  • instructions - Custom system prompt (optional, defaults to optimized prompt)
  • stopWhen - Condition to stop agent execution (optional, defaults to stepCountIs(5))
  • tools - Additional tools to include alongside Context7 tools (optional)
  • maxSteps - Maximum number of steps (deprecated, use stopWhen instead)
  • onStepFinish - Callback after each step

Basic Usage

Simple Query

With Custom API Key

Advanced Configuration

Custom Stop Condition

Custom Instructions

Adding Custom Tools

Agent Workflow

The agent follows this multi-step workflow:

Streaming Responses

Text Streaming

Full Stream with Tool Calls

Default System Prompt

The agent uses this optimized prompt by default:

Step Tracking

Step Callbacks

Error Handling

Complete Example

Comparison with Direct Tool Usage

Using Tools Directly

Using Context7Agent

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
  • You want to customize the tool selection logic

Type Exports

Best Practices

  1. Set reasonable step limits: Use stopWhen: stepCountIs(5) to prevent excessive API calls
  2. Use streaming for better UX: Stream responses to show progress to users
  3. Add custom tools carefully: Don’t overwhelm the agent with too many tools
  4. Monitor steps: Use onStepFinish to track agent behavior and debug issues
  5. Handle errors gracefully: Wrap generate() calls in try-catch blocks
  6. Reuse agent instances: Create one agent and use it for multiple queries

Next Steps

Tools Reference

Learn more about resolveLibraryId and queryDocs tools