Your DeepSeek request is receiving bytes, the connection remains open, and the answer area is empty. A watchdog says everything is healthy because its timer resets whenever data arrives. The person waiting for the answer sees a different problem: nothing useful has happened.
For a DeepSeek API stream that stays alive without output, the first distinction is between transport activity and model output. A keep-alive comment demonstrates recent activity on the connection you observe. It does not establish inference progress, explain the backend's state, or promise when an answer will arrive.
This article reconstructs a hypothetical incident in an application connected through an API relay. The timestamps and thresholds below are invented teaching examples, not measurements of DeepSeek or Treerouter. The practical question is narrow: when heartbeats continue, what should the application count as progress, and when should it stop waiting?
The incident begins with a healthy green indicator
Imagine an agent team's internal review tool. A developer submits one bounded text request using native DeepSeek Chat Completions. The application receives response headers, then several small chunks. Its status indicator stays green, yet no answer text appears.
At four seconds, an instrumented test client records : keep-alive. At eight seconds, it records the same comment. At eleven seconds, another arrives. The original watchdog updates one variable, last_activity, after every chunk. Because its idle threshold is five seconds, it has no reason to interrupt this particular sequence.
That behavior follows the watchdog's design. The mistake was treating its result as evidence that the requested work was advancing. Nobody had defined a separate limit for a request that remained connected without producing meaningful response data.
The incident log should preserve that distinction from the beginning. Record when the request started, when the first body bytes arrived, what kinds of frames followed, and whether a terminal marker was observed. Use a harmless synthetic prompt for a diagnostic capture. Frame types, lengths, and elapsed times often provide the needed evidence without copying prompt text or generated content into operational logs.
DeepSeek's request keep-alive documentation describes empty lines for non-streaming requests and SSE comments for streaming requests while a response is pending. It also states that the server closes a connection if inference has not started after ten minutes. That condition is not a universal request-duration limit or an application latency promise. A heartbeat alone cannot tell your application whether that condition currently applies.
For a non-streaming response, those empty lines are not separate JSON answers. Preserve the response body and parse its eventual JSON appropriately. For streaming, classify the comment without passing it to the JSON decoder as a model delta.
Reconstruct the wait with three clocks
The team replaces the overloaded activity variable with three measurements. All use elapsed time from a monotonic clock inside the receiving process, so a wall-clock adjustment cannot extend or shorten a deadline accidentally.
| Clock | What updates it | What it answers |
|---|---|---|
| Transport activity | Bytes received at the measured connection boundary | Has this connection recently delivered data? |
| Semantic progress | Accepted nonempty answer, reasoning, or tool-call deltas | Has the response advanced according to this application's policy? |
| Whole request | Starts once and never resets | Has this operation exhausted its allowed duration? |
The second clock requires an explicit product decision. In this example, a nonempty reasoning delta counts as response progress even if it is not displayed as answer text. A tool-call fragment also counts, but does not authorize execution of an incomplete tool call. A role announcement, empty delta, heartbeat, or usage-only update does not count.
Keep the first-answer timestamp separately if the interface needs it. A request can make semantic progress before producing visible answer text. Conversely, receiving the first answer word does not justify an unlimited wait for the rest. Resetting the progress clock permits an appropriate gap between meaningful updates; the whole-request clock still limits the total.
For the hypothetical sequence, heartbeats at four, eight, and eleven seconds keep the transport clock fresh. They leave the semantic clock at its initial value. At twelve seconds, an independent timer therefore records a no-progress outcome. Nothing about that decision says DeepSeek failed internally. It says the application's chosen waiting budget expired.
Those example limits are intentionally short so the fixtures are easy to inspect. Choose real limits from the workflow's waiting requirements and measured behavior, then document which component owns each one. An interactive drafting screen and a background report may have different budgets. If several layers impose limits, record them together: an application deadline cannot keep a request alive after an earlier proxy deadline has already ended it.
If the harness's underlying request format is unclear, establish it using the agent harness model-connection guide before changing timeout behavior. A parser expecting another protocol can misclassify valid progress as silence.
Test the classification without buying another generation
The WHATWG server-sent events specification defines colon-prefixed lines as comments. Blank lines delimit events, and multiple data lines contribute to an event's payload. A network chunk is therefore not necessarily a complete event: it can end halfway through a field or contain several events.
The following offline fixture deliberately starts after byte decoding and event assembly. It accepts complete, LF-delimited frames, uses synthetic elapsed timestamps, and makes no network request. It is a small policy test, not a replacement for a production SSE parser or SDK. Save it as keepalive_fixture.py and run it with Python 3.10 or later.
import json
from dataclasses import dataclass
def classify(frame):
if not frame.endswith("\n\n"):
return "protocol_error"
data = []
for line in frame[:-2].split("\n"):
if not line or line.startswith(":"):
continue
field, _, value = line.partition(":")
if field == "data":
data.append(value[1:] if value.startswith(" ") else value)
if not data:
return "transport"
payload = "\n".join(data)
if payload == "[DONE]":
return "done"
try:
obj = json.loads(payload)
if "error" in obj:
return "error"
choices = obj["choices"]
if not isinstance(choices, list):
return "protocol_error"
for choice in choices:
delta = choice.get("delta", {})
if delta.get("content") or delta.get("reasoning_content"):
return "progress"
for tool in delta.get("tool_calls") or []:
fn = tool.get("function") or {}
if tool.get("id") or fn.get("name") or fn.get("arguments"):
return "progress"
return "metadata"
except (ValueError, TypeError, KeyError, AttributeError):
return "protocol_error"
@dataclass
class Policy:
last_transport: float = 0
last_progress: float = 0
result: str | None = None
def step(self, elapsed, frame=None, eof=False):
if self.result:
return self.result
for expired, reason in (
(elapsed >= 30, "request_deadline"),
(elapsed - self.last_progress >= 12, "no_progress"),
(elapsed - self.last_transport >= 5, "transport_idle"),
):
if expired:
self.result = reason
return reason
if frame is not None:
self.last_transport = elapsed
kind = classify(frame)
if kind == "progress":
self.last_progress = elapsed
elif kind in {"done", "error", "protocol_error"}:
self.result = kind
if eof and not self.result:
self.result = "incomplete"
return self.result or "waiting"
heartbeat = ": keep-alive\n\n"
text = 'data: {"choices":[{"delta":{"content":"Hi"}}]}\n\n'
assert classify(heartbeat) == "transport"
assert classify('data: {"error":{}}\n\n') == "error"
assert classify("data: broken\n\n") == "protocol_error"
p = Policy()
for t in (4, 8, 11):
assert p.step(t, heartbeat) == "waiting"
assert p.step(12) == "no_progress"
assert Policy().step(5) == "transport_idle"
p = Policy()
for t in range(4, 29, 4):
assert p.step(t, text) == "waiting"
assert p.step(30, text) == "request_deadline"
assert Policy().step(1, text, eof=True) == "incomplete"
assert Policy().step(1, "data: [DONE]\n\n") == "done"
print("Offline fixtures passed")These assertions expose three different failures. Regular comments cannot reset the progress timer. Complete silence trips the transport timer. Regular text cannot extend the absolute deadline. The boundary rule is deliberate: a frame arriving at or after an expired limit cannot revive the operation.
The fixture also distinguishes a malformed payload from harmless whitespace and a partial answer from protocol completion. Its progress definition covers the response fields shown here; extend and validate that definition if your selected route introduces additional meaningful event types.
Add a regression fixture whenever an incident reveals a new boundary case. Useful additions include a role-only opening frame, reasoning before answer text, a fragmented tool argument, a finish-reason frame followed by EOF, and a late terminal marker. Keep parser tests separate from policy tests: first prove that bytes become the intended events, then prove that those events update only the intended clocks.
Move the clocks into the real receiving path
In production, update transport activity when bytes arrive, before waiting for a complete event. Otherwise a slowly assembled event could make your application report network inactivity even while bytes continue arriving. Incrementally decode UTF-8, handle supported line endings, and retain incomplete frames until their delimiters arrive. Put limits on retained data so a malformed stream cannot grow a buffer indefinitely.
The fixture's timer calls are explicit. A production deadline needs an independent timer or cancellation scope around the operation. Checking elapsed time only inside a loop body is insufficient: if the next read never returns, the loop never reaches that check.
HTTPX's timeout documentation distinguishes connect, read, write, and pool timeouts. Its read timeout limits the wait for a chunk of data. Consequently, regularly arriving heartbeat bytes can prevent a read timeout without satisfying your progress policy. Set transport timeouts and the outer request deadline separately; disabling the read timeout is not an implementation of either policy.
Use a cancellation mechanism that closes the local response and releases its connection when a deadline wins. Record the local outcome without assuming that upstream generation was cancelled or that no charge can occur. Keep the operation identifier and any available provider identifier for investigation.
At a relay boundary, a heartbeat establishes activity only on the connection being measured. A relay may handle comments differently from the native endpoint. Compare observations at identified boundaries before attributing a gap to a particular component. If actual text accumulates and then appears together, the separate stream-buffering investigation addresses that delivery symptom.
Close the incident with the right terminal condition
For native Chat Completions, DeepSeek's streaming response reference specifies data: [DONE] as the stream terminator. A choice's finish_reason describes why that choice ended; keep consuming the stream through its terminator and retain the final usage information. An empty delta or a finish reason alone should not make this fixture report a completed stream.
Protocol completion and application acceptance remain separate. A normally terminated response may still be truncated, contain a tool request requiring another workflow step, or fail your output validation. Record the finish reason alongside the transport outcome rather than compressing both into a single success flag.
Messages uses a different contract. DeepSeek's Anthropic API streaming guide identifies message_stop as the normal terminal event and explicitly says that this stream does not send [DONE]. It also documents a possible unnamed data frame containing an error object when waiting for generation times out. Inspect the payload for errors instead of assuming every error has a named SSE event. Do not reuse the Chat Completions fixture unchanged for Messages.
For this incident, the application records separate outcomes for a provider error, a malformed response, an unexpected connection close, a local timeout, and normal protocol completion. EOF before the expected terminator is incomplete. Preserve received content as partial, and communicate that state clearly to the user.
An HTTP failure before streaming starts belongs to another branch. Consult DeepSeek's error-code reference for authentication, balance, request-format, rate-limit, and service errors. A heartbeat is not an HTTP error code, and changing the timeout cannot repair an invalid credential.
Do not automatically launch another paid generation after partial output or an uncertain outcome. Review the existing operation first and require a deliberate recovery decision. The duplicate-request prevention guide covers that surrounding lifecycle, while the agent harness token-cost guide explains why additional model calls belong in task-level accounting.
Make a relay pilot answer this specific question
The hypothetical team closes its investigation when it can replay each outcome predictably. Its dashboard now distinguishes recent bytes, meaningful response progress, and remaining total time. The user-facing status can say that the connection is active while an answer is still pending, without claiming that the model is thinking or estimating a completion percentage.
Apply that same acceptance test when evaluating a DeepSeek API relay. Record the exact model identifier, operation URL, protocol, and selected route. Confirm which streaming events reach your client, which terminal condition applies, and how errors are represented. Use offline fixtures for failure cases before spending on a small live request.
The pilot's acceptance record should state what was actually observed. For example, it can establish that one bounded request returned meaningful deltas and the expected terminator through the chosen route. It cannot establish a guaranteed heartbeat interval, a backend queue position, or a service-wide availability figure. Keep those unmeasured properties out of both the incident conclusion and the purchasing decision.
After that request, reconcile its response with the available account record using the DeepSeek gateway billing check. A successful stream and an understandable charge answer different parts of the pilot. Neither establishes future latency or support for unrelated provider services.
Visit Treerouter to confirm the exact DeepSeek route and its streaming support, then create an account for a small pilot with your three clocks and explicit terminal checks. Expand usage once the observed behavior fits the waiting experience your application intends to provide.






