Skip to content

Use HttpCompletionOption.ResponseHeadersRead for faster response start #2

@GmausDev

Description

@GmausDev

Summary

Use HttpCompletionOption.ResponseHeadersRead when making HTTP requests to start processing the response as soon as headers are received, rather than waiting for the entire response body.

Current Behavior

var response = await _httpClient.PostAsJsonAsync("chat/completions", request, ...);

The default HttpCompletionOption.ResponseContentRead waits for the entire response before returning.

Proposed Change

var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

Expected Benefits

  • Faster response start - Begin processing immediately when headers arrive
  • Better memory efficiency - Stream response directly instead of buffering
  • Foundation for streaming - Required infrastructure for SSE/streaming support

Priority

🟡 P1 - High Impact

Technical Notes

  • Need to ensure proper disposal of response stream
  • Consider combining with ReadAsStreamAsync() for optimal memory usage
  • This change is a prerequisite for implementing streaming responses

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions