Skip to main content
This guide uses the legacy AgenticEngine, which runs a fixed query → evaluate → answer pipeline. For new projects, use Agentic Search, where the model decides when and what to search using tools.
Build powerful agentic RAG (Retrieval-Augmented Generation) applications that can intelligently search through your knowledge base, evaluate queries, and provide accurate answers. This guide walks you through creating a complete chat application using Agentset’s AgenticEngine with the AI SDK. The AgenticEngine provides an advanced agentic workflow that:
  • Generates relevant search queries from user messages
  • Retrieves and evaluates information from your knowledge base
  • Provides streaming responses with real-time status updates
  • Handles complex multi-step reasoning automatically

Prerequisites

Before getting started, make sure you have:
  • A Next.js application set up
  • An Agentset API key and namespace configured
  • An OpenAI API key for the language model
1

Install Dependencies

Install the required packages for building agentic RAG applications with AI SDK integration.
Key packages:
  • @agentset/ai-sdk: Provides the AgenticEngine for agentic RAG workflows
  • @ai-sdk/react: React hooks for chat functionality with streaming support
  • agentset: Core SDK for interacting with your knowledge base
  • ai: AI SDK core with UI message utilities
2

Configure Your Language Model

Set up your language model configuration that will be used by the AgenticEngine for query generation, evaluation, and answer synthesis.
lib/llm.ts
Make sure to add your OpenAI API key to your environment variables:
.env.local
3

Create the API Route

Create an API route that handles chat requests using the AgenticEngine with proper message validation and streaming support.
app/api/chat/route.ts
Key features:
  • Message validation: Ensures UI messages are properly formatted
  • Streaming response: Provides real-time updates during the agentic process
  • Error handling: Graceful error management with custom error messages
  • Three-step process: Query generation → evaluation → answer synthesis
4

Build the Frontend Chat Interface

Create a React component that provides a complete chat interface with support for agentic RAG status updates and query visibility.
app/page.tsx
Enhanced features:
  • Status updates: Shows real-time progress of the agentic process
  • Query visibility: Displays the generated search queries
  • Improved UX: Better styling and responsive design
  • Type safety: Uses AgentsetUIMessage type for better TypeScript support
5

Test Your Agentic RAG Application

Start your Next.js application and test the agentic RAG functionality:
Your chat application will now:
  1. Generate queries from user messages automatically
  2. Search your knowledge base using the generated queries
  3. Evaluate results and determine if more information is needed
  4. Synthesize answers based on retrieved information
  5. Stream responses with real-time status updates
Try asking complex questions that require multiple pieces of information from your knowledge base to see the agentic workflow in action.

Advanced Configuration

The AgenticEngine supports several optional parameters for fine-tuning:
  • maxEvals: Maximum number of evaluation rounds (default: 3)
  • tokenBudget: Token budget for the entire process (default: 4096)
  • queryOptions: Search configuration like topK, rerankLimit, and rerank settings

Next steps

  • API Reference — Search endpoint parameters and options
  • Citations — Add source attribution to responses
  • Ranking — Customize result ranking