<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: karlmoriaty</title>
    <description>The latest articles on DEV Community by karlmoriaty (@karlmoriaty).</description>
    <link>https://dev.to/karlmoriaty</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4269293%2Fe09a5694-1ae3-46f7-ba99-2d8adccd1331.jpg</url>
      <title>DEV Community: karlmoriaty</title>
      <link>https://dev.to/karlmoriaty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karlmoriaty"/>
    <language>en</language>
    <item>
      <title>Why your .NET AI request is slow: a segment-by-segment teardown</title>
      <dc:creator>karlmoriaty</dc:creator>
      <pubDate>Sat, 28 Nov 2026 11:41:57 +0000</pubDate>
      <link>https://dev.to/karlmoriaty/why-your-net-ai-request-is-slow-a-segment-by-segment-teardown-5f4o</link>
      <guid>https://dev.to/karlmoriaty/why-your-net-ai-request-is-slow-a-segment-by-segment-teardown-5f4o</guid>
      <description>&lt;p&gt;A support assistant answers in eight seconds. The HTTP span says &lt;code&gt;200 OK&lt;/code&gt;. The database span is fast. The LLM span is "slow". None of that tells you whether the time went into DNS, TLS, uploading the prompt, provider queueing, the first token, or streaming the answer.&lt;/p&gt;

&lt;p&gt;General APM was designed for request/response services. LLM calls have a different shape:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A long-lived streaming response, where time-to-first-token matters more than total duration.&lt;/li&gt;
&lt;li&gt;A cost dimension that general APM does not model.&lt;/li&gt;
&lt;li&gt;Provider-side queueing that looks like "network" unless you separate it.&lt;/li&gt;
&lt;li&gt;Retries that can silently double the bill.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Runesvaner is an in-process profiler for .NET AI workloads. This post walks through the measurement model and the numbers behind it, including what cannot be measured.&lt;/p&gt;

&lt;h2&gt;
  
  
  Six segments, measured where possible
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Segment&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Queue&lt;/td&gt;
&lt;td&gt;Middleware and serialization before the HTTP call&lt;/td&gt;
&lt;td&gt;Stopwatch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connect&lt;/td&gt;
&lt;td&gt;DNS + TCP connection&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SocketsHttpHandler.ConnectCallback&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Upload&lt;/td&gt;
&lt;td&gt;Request body serialization&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;HttpContent&lt;/code&gt; wrapper&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ProviderQueue&lt;/td&gt;
&lt;td&gt;Wait for response headers&lt;/td&gt;
&lt;td&gt;Stopwatch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TTFT&lt;/td&gt;
&lt;td&gt;First non-empty text delta&lt;/td&gt;
&lt;td&gt;MEAI decorator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generation&lt;/td&gt;
&lt;td&gt;Response stream until EOF&lt;/td&gt;
&lt;td&gt;Stream wrapper&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two details matter:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Connect is not TLS.&lt;/strong&gt; &lt;code&gt;ConnectCallback&lt;/code&gt; returns a plain stream; &lt;code&gt;SocketsHttpHandler&lt;/code&gt; performs TLS internally. There is no public TLS-complete hook in .NET. Runesvaner offers an opt-in TLS probe derived from certificate validation and marks it &lt;code&gt;Estimated=true&lt;/code&gt;/&lt;code&gt;Derived&lt;/code&gt;. It is off by default.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ProviderQueue is not TTFT.&lt;/strong&gt; Waiting for response headers is an HTTP-level signal. TTFT is the first token surfaced by the AI client.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The tests use a loopback TCP server and a real &lt;code&gt;HttpClient&lt;/code&gt;, so segments come from real sockets, not a mocked profiler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Streaming changes the question
&lt;/h2&gt;

&lt;p&gt;For streaming, total duration is misleading. The profiler records TTFT from the first non-empty delta, inter-token latency P50/P95 from text-chunk timestamps (&lt;code&gt;ItlEstimated=true&lt;/code&gt;), tokens/s only when usage exists, plus cancellation, interruption, and finish reason.&lt;/p&gt;

&lt;p&gt;A local SSE test compares recorded TTFT with a client-side observer. Local runs stay within 10 ms; CI uses a wider 50 ms budget because VM scheduling can add tens of milliseconds between async iterator steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cost: unknown is not zero
&lt;/h2&gt;

&lt;p&gt;Cost needs usage and price data. Providers report input/output/cached/reasoning tokens differently, prices change, and a model without a price entry must not silently cost zero.&lt;/p&gt;

&lt;p&gt;Runesvaner uses a versioned, checksummed price table. The built-in snapshot only contains entries whose source could be retrieved. At snapshot time, first-party OpenAI pricing pages returned HTTP 403, so those models resolve to &lt;code&gt;CostStatus.Unknown&lt;/code&gt; instead of a guessed number. The sample table is explicitly illustrative.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the numbers look like
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Measurement&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Recorder-only P95&lt;/td&gt;
&lt;td&gt;~3 µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Recorder allocation&lt;/td&gt;
&lt;td&gt;~1.5 KB per request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CI performance gate&lt;/td&gt;
&lt;td&gt;P95 ≤ 500 µs, allocation ≤ 2 KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Studio first screen, 10k JSONL records&lt;/td&gt;
&lt;td&gt;~0.84 s serial cold sum&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TTFT accuracy vs client observer&lt;/td&gt;
&lt;td&gt;within 10 ms locally&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The gate runs on every push. It measures the no-storage path; JSONL and SQLite writes are isolated behind a bounded background writer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three-line integration
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddRunesvaner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"anthropic"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StorageMode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StorageMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sqlite&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StoragePath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;".runesvaner"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ChatClientBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;innerChatClient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UseRunesvaner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serviceProvider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Content capture is &lt;code&gt;None&lt;/code&gt; by default. &lt;code&gt;Metadata&lt;/code&gt; records lengths and hashes; &lt;code&gt;Full&lt;/code&gt; stores text and is opt-in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Native AOT is a constraint
&lt;/h2&gt;

&lt;p&gt;The core packages avoid reflection and dynamic code, use JSON source generation, and are marked &lt;code&gt;IsAotCompatible&lt;/code&gt;. The sample publishes and runs under Native AOT on Windows and Linux in CI.&lt;/p&gt;

&lt;p&gt;SQLite is the exception: &lt;code&gt;Microsoft.Data.Sqlite&lt;/code&gt; AOT support is still open, so the AOT path uses JSONL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Repository: &lt;a href="https://github.com/karlmoriaty/runesvaner" rel="noopener noreferrer"&gt;https://github.com/karlmoriaty/runesvaner&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Documentation: &lt;a href="https://karlmoriaty.github.io/runesvaner/" rel="noopener noreferrer"&gt;https://karlmoriaty.github.io/runesvaner/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Compatibility matrix: &lt;a href="https://karlmoriaty.github.io/runesvaner/25-compatibility-matrix.html" rel="noopener noreferrer"&gt;https://karlmoriaty.github.io/runesvaner/25-compatibility-matrix.html&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If an AI request feels slow, start by separating the segments. The answer is usually one specific interval.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>ai</category>
      <category>observability</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
