Skip to content

feat: add response_size_bytes metric to chatd - #29816

Merged
cstyan merged 4 commits into
mainfrom
callum/chatd-response-size-bytes
Oct 29, 2026
Merged

cstyan merged 4 commits into
mainfrom
callum/chatd-response-size-bytes

Conversation

@cstyan

@cstyan cstyan commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

What

Add a coderd_chatd_response_size_bytes histogram recording the byte size of streamed model response content (assistant text, reasoning, and tool-call input deltas) per LLM request, labeled by provider and model.

Why

We had prompt_size_bytes (input) and tool_result_size_bytes but nothing for model output, so there was no way to measure output throughput of the chat system. With this metric, the coderd↔LLM input/output byte throughput is fully covered:

  • input: rate(coderd_chatd_prompt_size_bytes_sum[5m])
  • output: rate(coderd_chatd_response_size_bytes_sum[5m])

A histogram (rather than a counter) keeps it consistent with the existing size metrics and gives both throughput (_sum) and per-response size distribution.

Implementation

  • Bytes are accumulated from stream content deltas (TextDelta, ReasoningStart/ReasoningDelta, ToolInputDelta) in processStepStream and recorded once per step, at the same call site as prompt_size_bytes.
  • Measures response content bytes (consistent with prompt_size_bytes), not HTTP/SSE wire framing.
  • Tool results are intentionally excluded (already covered by tool_result_size_bytes).

Tests

  • metrics_test.go: registration + NopMetrics coverage.
  • chatd_test.go (TestActiveServer_RecordsGenerationMetrics): asserts the metric is recorded with a positive sum on a streamed response.

Opened by Coder Agents on behalf of @cstyan.

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Docs preview

Check off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here.

Comment thread coderd/x/chatd/chatloop/chatloop.go Outdated
Comment on lines +378 to +387
}
return AssistantOutcome{}, wrappedErr
}
opts.Metrics.ResponseSizeBytes.WithLabelValues(provider, modelName).Observe(float64(result.responseBytes))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're recording after the error check, we are technically missing response bytes if the response was interrupted, for whatever reason. Could we record them all?

@ethanndickson

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-02T07:36:23.723591Z 7c43e36 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c43e36733

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/chatloop/chatloop.go Outdated
}

case fantasy.StreamPartTypeToolInputDelta:
result.responseBytes += len(part.Delta)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Count boundary deltas in tool-input sizes

When a provider places tool-input bytes in tool_input_start or tool_input_end, this only increments the metric for tool_input_delta, so tool-heavy responses are undercounted. This is a supported stream shape exercised in coderd/x/chatd/chatdebug/model_normalization_internal_test.go:179-184, where both boundary events carry distinct argument chunks; count part.Delta for all three incremental tool-input event types without counting the final ToolCall summary again.

Useful? React with 👍 / 👎.

Comment thread coderd/x/chatd/chatloop/chatloop.go Outdated
}
return AssistantOutcome{}, wrappedErr
}
opts.Metrics.ResponseSizeBytes.WithLabelValues(provider, modelName).Observe(float64(result.responseBytes))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Record bytes from failed streaming attempts

When a stream emits text, reasoning, or tool-input chunks and then returns an error, processStepStream returns the accumulated byte count but the error branch above exits before this observation. Those bytes crossed the model boundary and the documented per-LLM-request histogram should include them; otherwise _sum underreports output throughput and _count no longer corresponds to requests during provider failures or retries.

Useful? React with 👍 / 👎.

@cstyan
cstyan force-pushed the callum/chatd-response-size-bytes branch from e75bc9d to f276efb Compare October 5, 2026 21:29
@cstyan
cstyan requested a review from ethanndickson October 5, 2026 21:32
@cstyan

cstyan commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

@ethanndickson updated both the observation location (before potential errors) as well as the counting of result bytes for additional results that were missed before. For the latter, the current expected behaviour is really just checked via the newest test, and I left a comment to that effect as well.

@ethanndickson ethanndickson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sick!

Comment on lines +975 to +979
// Counts Delta on every part type. This relies on providers keeping
// summary data, such as ToolCall input, out of Delta. Only
// TestGenerateAssistant_ResponseSizeBytesByPartType checks this, so
// update it when adding new part types.
result.responseBytes += len(part.Delta)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Counts Delta on every part type. This relies on providers keeping
// summary data, such as ToolCall input, out of Delta. Only
// TestGenerateAssistant_ResponseSizeBytesByPartType checks this, so
// update it when adding new part types.
result.responseBytes += len(part.Delta)
result.responseBytes += len(part.Delta)

agent yap

@github-actions github-actions Bot added the stale This issue is like stale bread. label Oct 26, 2026
Add a coderd_chatd_response_size_bytes histogram recording the byte size
of streamed model response content (assistant text, reasoning, and
tool-call input deltas) per LLM request, labeled by provider and model.

Together with the existing prompt_size_bytes and tool_result_size_bytes
histograms, this completes the coderd<->LLM input/output byte throughput
picture (rate(..._sum) gives bytes/s). Bytes are accumulated from stream
content deltas and recorded once per step, mirroring prompt_size_bytes.

Generated with Coder Agents.
…processing fails

Observe response size before handling stream errors so responses that fail partway are still counted.
…deltas

Count Delta on every stream part so tool input bytes on tool_input_start and tool_input_end are included. The ToolCall summary is not counted since it repeats the streamed tool input.
@cstyan
cstyan force-pushed the callum/chatd-response-size-bytes branch from f276efb to e4b87b4 Compare October 28, 2026 22:21
@cstyan cstyan changed the title feat(coderd/x/chatd): add response_size_bytes metric feat: add response_size_bytes metric to chatd Oct 28, 2026
@github-actions github-actions Bot removed the stale This issue is like stale bread. label Oct 29, 2026
@cstyan
cstyan merged commit 2255794 into main Oct 29, 2026
32 of 33 checks passed
@cstyan
cstyan deleted the callum/chatd-response-size-bytes branch October 29, 2026 01:35
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants