docs/en/Community-Articles/2025-11-22-building-production-ready-llm-applications/post.md
Large Language Models (LLMs) have evolved rapidly, and integrating them into production .NET applications requires staying current with the latest approaches. In this article, I'll share practical tips and patterns I've learned while building LLM-powered systems, covering everything from API changes in GPT-5 to implementing efficient RAG (Retrieval Augmented Generation) architectures.
Whether you're building a chatbot, a knowledge base assistant, or integrating AI into your enterprise applications, these production-tested insights will help you avoid common pitfalls and build more reliable systems.
If you've been working with GPT-4 or earlier models, you're familiar with the temperature and top_p parameters for controlling response randomness. Here's the critical update: GPT-5 no longer supports these parameters!
var chatRequest = new ChatOptions
{
Temperature = 0.7, // ✅ Worked with GPT-4
TopP = 0.9 // ✅ Worked with GPT-4
};
var chatRequest = new ChatOptions
{
RawRepresentationFactory = (client => new ChatCompletionOptions()
{
#pragma warning disable OPENAI001
ReasoningEffortLevel = "minimal",
#pragma warning restore OPENAI001
})
};
Why the change? GPT-5 incorporates an internal reasoning and verification process. Instead of controlling randomness, you now specify how much computational effort the model should invest in reasoning through the problem.
Pro Tip: Reasoning tokens are included in your API costs. Use "High" only when necessary to optimize your budget.
Here's a critical insight that can save you hours of debugging: Important rules must be repeated at the END of your prompt!
You are a helpful assistant.
RULE: Never share passwords or sensitive information.
[User Input]
You are a helpful assistant.
RULE: Never share passwords or sensitive information.
[User Input]
⚠️ REMINDER: Apply the rules above strictly, ESPECIALLY regarding passwords.
Why? LLMs suffer from the "Lost in the Middle" phenomenon—they pay more attention to the beginning and end of the context window. Critical instructions buried in the middle are often ignored.
Retrieval Augmented Generation (RAG) is essential for grounding LLM responses in your own data. The most effective pattern I've found is the Parent-Child approach.
Split documents into hierarchies:
Store both in vector database with references
Query flow:
Always use overlapping chunks to prevent information loss at boundaries!
Chunk 1: Token 0-500
Chunk 2: Token 400-900 ← 100 token overlap
Chunk 3: Token 800-1300 ← 100 token overlap
Standard recommendation: 10-20% overlap (for 500 tokens, use 50-100 token overlap)
using Microsoft.SemanticKernel.Text;
var chunks = TextChunker.SplitPlainTextParagraphs(
documentText,
maxTokensPerParagraph: 500,
overlapTokens: 50
);
foreach (var chunk in chunks)
{
var embedding = await embeddingService.GenerateEmbeddingAsync(chunk);
await vectorDb.StoreAsync(chunk, embedding);
}
For .NET developers, choosing a vector database can be overwhelming. After evaluating multiple options, PostgreSQL with pgvector is the most practical choice for most scenarios.
✅ Use existing SQL knowledge - No new query language to learn
✅ EF Core integration - Works with your existing data access layer
✅ JOIN with metadata - Combine vector search with traditional queries
✅ WHERE clause filtering - Filter by tenant, user, date, etc.
✅ ACID compliance - Transaction support for data consistency
✅ No separate infrastructure - One database for everything
First, install the NuGet package:
dotnet add package Pgvector.EntityFrameworkCore
Define your entity:
using Pgvector;
using Pgvector.EntityFrameworkCore;
public class DocumentChunk
{
public Guid Id { get; set; }
public string Content { get; set; }
public Vector Embedding { get; set; } // 👈 pgvector type
public Guid ParentChunkId { get; set; }
public DateTime CreatedAt { get; set; }
}
Configure in DbContext:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.HasPostgresExtension("vector");
builder.Entity<DocumentChunk>()
.Property(e => e.Embedding)
.HasColumnType("vector(1536)"); // 👈 OpenAI embedding dimension
builder.Entity<DocumentChunk>()
.HasIndex(e => e.Embedding)
.HasMethod("hnsw") // 👈 Fast approximate search
.HasOperators("vector_cosine_ops");
}
using Pgvector.EntityFrameworkCore;
public async Task<List<DocumentChunk>> SearchAsync(string query)
{
// 1. Convert query to embedding
var queryVector = await _embeddingService.GetEmbeddingAsync(query);
// 2. Search
return await _context.DocumentChunks
.OrderBy(c => c.Embedding.L2Distance(queryVector)) // 👈 Lower is better
.Take(5)
.ToListAsync();
}
Source: Pgvector.NET on GitHub
A common mistake is calling RAG on every single user message. This wastes tokens and money. Instead, make RAG a tool and let the LLM decide when to use it.
// Always call RAG, even for "Hello"
var context = await PerformRAG(userMessage);
var response = await chatClient.CompleteAsync($"{context}\n\n{userMessage}");
[KernelFunction]
[Description("Search the company knowledge base for information")]
public async Task<string> SearchKnowledgeBase(
[Description("The search query")] string query)
{
var results = await _vectorDb.SearchAsync(query);
return string.Join("\n---\n", results.Select(r => r.Content));
}
The LLM will call SearchKnowledgeBase only when needed:
When your documents are in one language (e.g., English) but users query in another (e.g., Turkish), you need a translation strategy.
Option 1: Use an LLM that automatically calls tools in English
Option 2: Tool chain approach
[KernelFunction]
[Description("Translate text to English")]
public async Task<string> TranslateToEnglish(string text)
{
// Translation logic
}
[KernelFunction]
[Description("Search knowledge base (English only)")]
public async Task<string> SearchKnowledgeBase(string englishQuery)
{
// Search logic
}
The LLM will:
TranslateToEnglish("2024 geliri nedir?")SearchKnowledgeBase("What was 2024 revenue?")Microsoft and Anthropic recently released official C# SDKs for the Model Context Protocol (MCP). This is a game-changer for tool reusability.
| Feature | SK Plugins | MCP Servers |
|---|---|---|
| Process | In-process | Out-of-process (stdio/http) |
| Reusability | Application-specific | Cross-application |
| Examples | Used within your app | VS Code Copilot, Claude Desktop |
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Extensions.Hosting;
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
builder.Services.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
Define your tools:
[McpServerToolType]
public static class FileSystemTools
{
[McpServerTool, Description("Read a file from the file system")]
public static async Task<string> ReadFile(string path)
{
// ⚠️ SECURITY: Always validate paths!
if (!IsPathSafe(path))
throw new SecurityException("Invalid path");
return await File.ReadAllTextAsync(path);
}
private static bool IsPathSafe(string path)
{
// Implement path traversal prevention
var fullPath = Path.GetFullPath(path);
return fullPath.StartsWith(AllowedDirectory);
}
}
Your MCP server can now be used by VS Code Copilot, Claude Desktop, or any other MCP client!
For long conversations, storing all history in the context window becomes impractical. Here's the pattern that works:
First 50 messages → Summarize with LLM → Single summary message
Problem: Detail loss (fidelity loss)
[KernelFunction]
[Description("Search conversation history for past discussions")]
public async Task<string> SearchChatHistory(
[Description("What to search for")] string query)
{
var relevantMessages = await _vectorDb.SearchAsync(query);
return string.Join("\n", relevantMessages.Select(m =>
$"[{m.Timestamp}] {m.Role}: {m.Content}"));
}
The LLM retrieves only relevant past context when needed, avoiding summary-induced information loss.
A common misconception is using fine-tuning for knowledge injection. Here's when to use each:
| Purpose | RAG | Fine-Tuning |
|---|---|---|
| Goal | Memory (provide facts) | Behavior (teach style) |
| Updates | Dynamic (add docs anytime) | Static (requires retraining) |
| Cost | Low dev, higher inference | High dev, lower inference |
| Hallucination | Reduces | Doesn't reduce |
| Use Case | Company docs, FAQs | Brand voice, specific format |
Common mistake: "Let's fine-tune on our company documents" ❌
Better approach: Use RAG! ✅
Fine-tuning is for teaching the model how to respond, not what to know.
Source: Oracle - RAG vs Fine-Tuning
When using LLMs to generate diagrams and visualizations, always request SVG format instead of PNG or JPG.
✅ Text-based → LLMs produce better results
✅ Lower cost → Fewer tokens than base64-encoded images
✅ Editable → Easy to modify after generation
✅ Scalable → Perfect quality at any size
✅ Version control friendly → Works great in Git
Create an architecture diagram showing PostgreSQL with pgvector integration.
Format: SVG, 800x400 pixels. Show: .NET Application → EF Core → PostgreSQL → Vector Search.
Use arrows to connect stages. Color scheme: Blue tones.
All diagrams in this article were generated as SVG, resulting in excellent quality and lower token costs!
Pro Tip: If you don't need photographs or complex renders, always choose SVG.
Here's the recommended stack for building production LLM applications with .NET:
Let me summarize the most important production tips:
ReasoningEffortLevel with GPT-5Happy coding! 🚀