streaming-response-handler
Skill0 starsChanged 6 months agoMITView on GitHub
Streams LLM responses chunk-by-chunk from an agent via runner.run_stream(). Use when building real-time UIs, SSE endpoints, WebSocket streaming, or any feature that needs to display tokens as they arrive rather than waiting for the full response.
In this repository65 other files it gives its agents.
- AGENTS.mdAGENTS.md
- skills/building-agents/agents.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms-full.txtllms.txt
- llms.txtllms.txt
- adding-guardrailsSkill
- agent-health-timeoutSkill
- agent-permission-scopingSkill
- agent-session-isolationSkill
- agent-step-loggingSkill
- agent-supervisor-routerSkill
- agent-telemetrySkill
- async-agent-backgroundSkill
- building-agentsSkill
- building-teamsSkill
- building-toolsSkill
- calendar-scheduling-toolSkill
- callback-tracing-handlerSkill
- chain-of-thought-promptingSkill
- chat-model-configurationSkill
- code-execution-sandboxSkill
- common-agent-patternsSkill
- context-window-managementSkill
- conversation-memorySkill
- conversation-state-dbSkill
- database-query-toolSkill
- document-loadersSkill
- document-rerankingSkill
- email-notification-toolSkill
- embedding-model-ingestionSkill
- fallback-model-chainSkill
- few-shot-injectionSkill
- filesystem-toolSkill
- human-in-the-loopSkill
- hybrid-searchSkill
- inspecting-streamsSkill
- integrating-with-laurenSkill
- json-schema-enforcementSkill
- llm-provider-connectionSkill
- llm-rate-limitingSkill
- long-term-vector-memorySkill
- managing-memorySkill
- migrating-to-lauren-aiSkill
- multi-agent-delegationSkill
- multi-tool-orchestrationSkill
- multi-turn-tool-stateSkill
- output-guardrailsSkill
- plan-and-execute-agentSkill
- prompt-injection-defenseSkill
- prompt-versioningSkill
- rag-pipelineSkill
- react-agent-loopSkill
- response-validation-retrySkill
- rest-api-toolSkill
- securing-agentsSkill
- semantic-cacheSkill
- structured-output-parsingSkill
- sub-agent-spawningSkill
- system-prompt-templatingSkill
- testing-agentsSkill
- The first 60 shown.
The file
Files
Use
codemap find "SymbolName"to locate any symbol before reading — it gives exact file + line range and is faster than grep across the whole repo.
Streaming Response Handler
Quick start
# from __future__ import annotations is safe here (no @tool definitions)
from __future__ import annotations
from lauren_ai._agents._runner import AgentRunnerBase as AgentRunner
from lauren_ai._config import LLMConfig
from lauren_ai._transport._mock import MockTransport
cfg = LLMConfig(provider="anthropic", model="claude-opus-4-6", api_key="sk-ant-...")
runner = AgentRunner(transport=transport)
async for chunk in await runner.run_stream(my_agent_instance, "Tell me a joke"):
if chunk.delta:
print(chunk.delta, end="", flush=True)
if chunk.stop_reason:
print() # newline at endCompletionChunk fields
| Field | Type | Description |
delta | str | Text fragment from this chunk (may be empty string) |
stop_reason | str | None | Set on the final chunk: "end_turn", "tool_use", etc. |
usage | TokenUsage | None | Set on the final chunk with token counts |
thinking_delta | str | None | Anthropic extended thinking fragment |
tool_call_delta | ToolCallDelta | None | Streaming tool-call fragment |
guardrail_override | str | None | Set if a guardrail replaced the output |
Accumulate full text
async def stream_and_collect(runner, agent_instance, prompt):
full_text = ""
async for chunk in await runner.run_stream(agent_instance, prompt):
if chunk.delta:
full_text += chunk.delta
return full_textLauren SSE integration
from lauren import sse, EventStream
from lauren_ai._agents._runner import AgentRunnerBase as AgentRunner
@get("/chat/stream")
async def chat_stream(
prompt: Query[str],
agent: MyAgent,
runner: AgentRunner,
) -> EventStream:
async def _generate():
async for chunk in await runner.run_stream(agent, prompt):
if chunk.delta:
yield chunk.delta
return EventStream(_generate())Testing pattern — queue stream chunks
from lauren_ai._transport import CompletionChunk, TokenUsage
from lauren_ai._transport._mock import MockTransport
mock = MockTransport()
mock.queue_stream([
CompletionChunk(delta="Hello"),
CompletionChunk(delta=" world"),
CompletionChunk(
stop_reason="end_turn",
usage=TokenUsage(input_tokens=5, output_tokens=2),
),
])
chunks = []
async for chunk in await runner.run_stream(my_agent, "hi"):
chunks.append(chunk)Reference files
| File | Contents |
src/lauren_ai/_agents/_runner.py | run_stream() implementation |
src/lauren_ai/_transport/__init__.py | CompletionChunk, ToolCallDelta |
src/lauren_ai/_transport/_mock.py | queue_stream() helper |
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

