DEV Community

Cover image for Building a RAG Agent with .NET Aspire
Tostisto
Tostisto

Posted on

Building a RAG Agent with .NET Aspire

Retrieval-augmented generation sounds complicated, but the core idea fits in a small app: find the right pieces of your docs, hand them to a model, and let it answer from them. This post walks through a small example that does exactly that, running locally with Aspire, Ollama and Qdrant.

The example

The app reads a folder of Markdown pages, splits them into chunks and stores them in Qdrant. When you ask a question, an agent searches those chunks and answers only from what it found, with links to the pages it used.

The sample content is the VS Code wiki, pulled into the repo as a git submodule. It is real documentation, but small enough to index quickly.

Everything runs on your machine:

  • Aspire starts all the pieces with a single command.
  • Ollama runs the models: llama3.2:3b for chat and nomic-embed-text for embeddings.
  • Qdrant holds the vectors.
  • Microsoft.Extensions.AI, Microsoft.Extensions.VectorData and Microsoft Agent Framework tie it together in .NET.

The full code is on GitHub: Tostisto/dotnet-aspire-rag.

How it fits together

Architecture overview

Aspire starts Qdrant, Ollama and both apps. The ingestion worker runs first and exits, and only then does the API start, so it never answers from an empty index.

Inside each app the code follows Clean Architecture. The core only knows small interfaces like IKnowledgeBase and IAssistant, while Qdrant, Ollama and the agent framework live in the Infrastructure project. A few architecture tests break the build if that rule is ever broken.

Two ways to retrieve

There are two common ways to wire retrieval into an agent, and the API lets you pick one per request with the mode field:

  • Always is classic RAG. Search first, then put the results into the prompt.
  • OnDemand is agentic RAG. The model gets a search_docs tool and decides for itself when to use it.

Agent Framework's TextSearchProvider handles both, so switching is a single option:

var searchProvider = new TextSearchProvider(
    search,
    new TextSearchProviderOptions
    {
        SearchTime = strategy is RetrievalStrategy.OnDemand
            ? TextSearchBehavior.OnDemandFunctionCalling
            : TextSearchBehavior.BeforeAIInvoke,
        FunctionToolName = "search_docs",
    });

var agent = chatClient.AsAIAgent(new ChatClientAgentOptions
{
    ChatOptions = new ChatOptions { Instructions = AssistantPolicy.Instructions },
    AIContextProviders = [searchProvider],
});
Enter fullscreen mode Exit fullscreen mode

The search function behind it calls Qdrant and remembers which pages came back. Those pages become the citations in the answer.

Lessons learned

The biggest surprise was how much the choice of model mattered. In OnDemand mode the first model often skipped the search completely and just replied "I don't know". To see how bad it was, the same prompt and tool went to three local models, with eight questions the wiki can answer:

Model Searched (of 8) Time per decision
qwen3:4b-instruct 2 under 1 s
qwen3:4b (thinking) 8 about 5 s
llama3.2:3b 8 about 0.2 s

No amount of prompt tweaking fixed qwen3:4b-instruct. Switching to llama3.2:3b did, and since then the integration tests pass in both modes.

A few other things worth knowing:

  • Thinking models are slow for chat. The thinking variant of qwen3 took 10 to 20 seconds per answer, the instruct one about a second.
  • Chunks need context. Many wiki pages have no title heading, so each chunk now starts with its page title and heading path, like Coding Guidelines > Names.
  • Some words are just noise. Every page mentions VS Code, so a question containing "VS Code" kept pulling in roadmap pages. The same question without those two words found the right section.
  • Hybrid search did not help here. Keyword matching on words like "code" hits almost every page, so it made results worse and was taken out.
  • Local models need longer timeouts. Aspire's default of 30 seconds cut off slower model calls.
  • Tests need stable answers. Temperature 0 and a fixed seed made the integration tests repeatable.

Try it

You need the .NET 10 SDK, Docker, Ollama and the Aspire CLI.

git clone --recurse-submodules https://github.com/Tostisto/dotnet-aspire-rag.git
cd dotnet-aspire-rag
aspire run
Enter fullscreen mode Exit fullscreen mode

The first run downloads the models, so give it a few minutes. Then open the Scalar link on the api resource in the Aspire dashboard and ask something:

POST /api/ask
{ "question": "Do we indent code with tabs or spaces?", "mode": "OnDemand" }
Enter fullscreen mode Exit fullscreen mode

You get the answer together with the page it came from: "We use tabs, not spaces [Coding Guidelines]." The dashboard shows each model call, tool call and search as a trace, which is the easiest way to see what the agent actually did.

Want to try it on your own docs? Add a folder of Markdown files under Documents:Folders in the AppHost settings.

Top comments (0)