> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentset.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Simple RAG

> Build retrieval-augmented generation with Agentset

Combine Agentset search with an LLM to generate answers grounded in your documents. This pattern retrieves relevant context from your namespace and passes it to the model.

## Basic RAG pattern

Search your namespace, format the results as context, and generate a response.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { Agentset } from "agentset";
  import { generateText } from "ai";
  import { openai } from "@ai-sdk/openai";

  const agentset = new Agentset({
    apiKey: process.env.AGENTSET_API_KEY,
  });

  const ns = agentset.namespace("YOUR_NAMESPACE_ID");

  const query = "What are the key findings?";

  // Search for relevant context
  const results = await ns.search(query);
  const context = results.map((r) => r.text).join("\n\n");

  // Generate a response
  const { text } = await generateText({
    model: openai("gpt-5.1"),
    system: `Answer questions based on the following context:\n\n${context}`,
    prompt: query,
  });

  console.log(text);
  ```

  ```python Python theme={null}
  import os
  from agentset import Agentset
  from openai import OpenAI as OpenAIClient

  client = Agentset(
      namespace_id="YOUR_NAMESPACE_ID",
      token=os.environ["AGENTSET_API_KEY"],
  )

  openai = OpenAIClient()

  query = "What are the key findings?"

  # Search for relevant context
  results = client.search.execute(query=query)
  context = "\n\n".join([r.text for r in results.data])

  # Generate a response
  response = openai.responses.create(
      model="gpt-5.1",
      input=[
          {
              "role": "system",
              "content": f"Answer questions based on the following context:\n\n{context}",
          },
          {
              "role": "user",
              "content": query,
          },
      ],
  )

  print(response.output_text)
  ```
</CodeGroup>

## Filtering context

Narrow results to specific documents using [metadata filters](/search-and-retrieval/filtering).

<CodeGroup>
  ```typescript TypeScript theme={null}
  const results = await ns.search(query, {
    filter: {
      category: "technical",
      year: { $gte: 2024 },
    },
  });
  ```

  ```python Python theme={null}
  results = client.search.execute(
      query=query,
      filter={
          "category": "technical",
          "year": {"$gte": 2024},
      },
  )
  ```
</CodeGroup>

## Next steps

* [API Reference](/api-reference/endpoint/search) — Search endpoint parameters and options
* [Citations](/search-and-retrieval/citations) — Add source attribution to generated responses
* [AI SDK Integration](/search-and-retrieval/ai-sdk-integration) — Use Agentset with Vercel AI SDK
