r/dotnet • u/kralmoriaty • 27m ago
Article I built an in-process latency/cost profiler for .NET AI apps — here is the segment model and what it cannot measure
I maintain Runesvaner, a small Apache-2.0 .NET library that records AI requests as a latency waterfall plus a cost ledger. It is not a gateway and not a replacement for OpenTelemetry; it is an in-process decorator for IChatClient, Semantic Kernel, and VectorData.
The motivation was simple: a RAG endpoint took eight seconds, the LLM span was "slow", and none of the existing tooling told me whether the time was in DNS/TLS, upload, provider queueing, the first token, or streaming.
The measurement model splits a call into:
- Queue — middleware/serialization before HTTP
- Connect — DNS + TCP via
ConnectCallback - Upload — request body serialization
- ProviderQueue — wait for response headers
- TTFT — first non-empty text delta
- Generation — response stream until EOF
Two honesty notes:
- TLS has no public completion hook in .NET. I offer an opt-in probe that derives it from certificate validation and marks it
Estimated=true/Derived. It is off by default. - Inter-token latency is derived from text chunks, not provider tokens, and is marked
ItlEstimated=true. Tokens/s isnullwithout usage.
Cost is versioned and deliberately conservative. The built-in snapshot only includes prices whose source could be retrieved. OpenAI first-party pricing pages returned 403 at snapshot time, so those models resolve to Unknown, not zero. Sample prices are labeled illustrative.
Current state: 100 tests, 10 NuGet packages at 0.3.0-alpha.1 (preview being prepared), Windows/Linux CI, and Windows/Linux Native AOT smoke. Studio can browse a 10k-record JSONL set with a ~0.84 s cold first screen. SQLite AOT is still a known risk, so the AOT path uses JSONL.
Known gaps: budget alerts currently fire as events/callbacks but are not persisted to Studio; the Semantic Kernel adapter does not record token usage because SK Abstractions 1.82 has no unified usage type; tail sampling weights cost totals but not request counts.
Links:
- Repo: https://github.com/karlmoriaty/runesvaner
- Docs: https://karlmoriaty.github.io/runesvaner/
- Compatibility: https://karlmoriaty.github.io/runesvaner/25-compatibility-matrix.html
I would like feedback from people running AI workloads in .NET: how do you currently separate provider queueing from your own upload/serialization time? And would a segment-level waterfall be useful in your incident reviews?


