Prompt Progress
Interim progress of a prompt, for clients that stream the prompt api’s response.
A prompt can take a while: the LLM may call tools, plugins and MCP servers, and each tool call
adds another round trip to the LLM. A client that sends Accept: text/event-stream to a
LLMClient’s prompt api receives Server-Sent Events (SSE) while the prompt runs, followed by the
same JSON that a non-streaming client receives. See
smarter.apps.llmclient.api.v1.views.base.LLMClientApiBaseViewSet.
The prompt itself runs synchronously, exactly as it does for a non-streaming client, in a thread
of the prompts’ own pool, prompt_executor(). Its progress comes from the signals that the
prompt already sends (chat_request, llm_tool_requested, chat_plugin_called,
mcpclient_tool_called, …). The receivers in this module forward them to the sink of the
prompt that sent them, which is held in a
contextvars.ContextVar, so that concurrent prompts never see each other’s events.
SSE payload format
retry: 3000is sent once, first.event: progresswithdata: {"type": ..., "message": ..., ...}for each step.: keepalivecomments while the prompt is idle, e.g. waiting on the LLM.event: resultwithdata: {"status": <http status>, "response": <the api's json>}, last.
- class smarter.apps.prompt.progress.PromptProgressEvents[source]
Bases:
objectThe
typeof each progress event.- LLM_REQUEST = 'llm_request'
- MCP_TOOL_CALLED = 'mcp_tool_called'
- MCP_TOOL_FAILED = 'mcp_tool_failed'
- MCP_TOOL_RESPONDED = 'mcp_tool_responded'
- PLUGIN_CALLED = 'plugin_called'
- TOOL_REQUESTED = 'tool_requested'
- TOOL_RESPONDED = 'tool_responded'
- smarter.apps.prompt.progress.emit(event_type, message, **detail)[source]
Send a progress event to the current prompt’s sink, if it has one.
A prompt without a streaming client has no sink, so this does nothing. A failing sink is logged, and never fails the prompt.
- Parameters:
event_type (
str) – One ofPromptProgressEvents.message (
str) – A short, human-readable description of the step.detail (
Any) – Other JSON-serializable fields of the event.
- Return type:
- smarter.apps.prompt.progress.progress_sink(sink)[source]
Send the progress events of the prompt that runs in this context to
sink.
- async smarter.apps.prompt.progress.prompt_event_stream(complete, on_error)[source]
Run a prompt in a thread of
prompt_executor(), and yield its progress, then its result, as SSE frames.- Parameters:
complete (
Callable[[],HttpResponse]) – Runs the prompt synchronously, and returns the prompt api’s JSON response.on_error (
Callable[[Exception],HttpResponse]) – Returns the prompt api’s JSON error response for an exception thatcompleteraised.
- Yields:
SSE frames: progress events and keepalives, then the result.
- Return type:
AsyncIterator[str]
- smarter.apps.prompt.progress.prompt_event_stream_response(complete, on_error)[source]
The prompt api’s response as Server-Sent Events.
See
prompt_event_stream().completeandon_errorrun in the request’s log context, so that the prompt’s log records reach the user’s server log stream, tagged with the request’s id. Seesmarter.lib.logging.redis_log_handler.bind_log_context().- Parameters:
complete (
Callable[[],HttpResponse]) – Runs the prompt synchronously, and returns the prompt api’s JSON response.on_error (
Callable[[Exception],HttpResponse]) – Returns the prompt api’s JSON error response for an exception thatcompleteraised.
- Returns:
A streaming
text/event-streamresponse, whose http status is always 200. The prompt’s own status is in itsresultevent.- Return type:
- smarter.apps.prompt.progress.prompt_executor()[source]
Return the thread pool that runs this process’s streamed prompts, creating it on first use.
Prompts don’t run in the event loop’s default executor, whose size is
min(32, CPUs + 4), 6 threads on a 2-vCPU node. A prompt holds its thread for as long as it runs, waiting on the LLM and its tools, so a handful of concurrent prompts filled it, and queued everything else that uses it: Django’s error pages, whichsync_to_async(..., thread_sensitive=False)renders there, and asyncio’s host name lookups. Sharing its threads also shared their database connections: a connection that an error page left open was reused by the next prompt in the same thread, hours later, after MariaDB had closed it (seesmarter.lib.django.http.error_handlers).The pool has
smarter_settings.prompt_threadsthreads, namedsmarter-prompt-<n>. It is created on first use, so that a process that never streams a prompt, e.g. a Celery worker or a management command, never starts it.- Returns:
The process’s prompt thread pool.
- Return type:
- smarter.apps.prompt.progress.wants_event_stream(request)[source]
True if the client asked for the prompt api’s response as Server-Sent Events.
- Parameters:
request (
HttpRequest) – The prompt api’s request.- Returns:
True if the request’s Accept header includes
text/event-stream.- Return type: