Disabling message streaming
Granularly control what is streamed to the frontend.
What is this?
Some LLM calls in a flow must stay private, such as a call that reads other employees' records. Call acompletion directly for those, and wrap only the calls you want streamed in copilotkit_stream. copilotkit_stream streams both messages and tool calls to the frontend.
When should I use this?
Occasionally, you'll want to disable streaming temporarily. For example, the LLM may be doing something the current user should not see, like emitting tool calls or questions pertaining to other employees in an HR system.
Implementation
Disable all streaming
You can control whether to stream messages or tool calls by selectively wrapping calls to acompletion with copilotkit_stream.
from ag_ui_crewai import copilotkit_stream
from litellm import acompletion
@start()
async def start(self):
# 1) Do not emit messages or tool calls, keeping the LLM call private.
response = await acompletion(
reasoning_effort="none",
allowed_openai_params=["reasoning_effort"],
model="openai/gpt-6-luna",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
*self.state.messages
],
)
message = response.choices[0].message
# 2) Or wrap the LLM call with `copilotkit_stream` to stream message tokens.
# Note that we pass `stream=True` to the inner `acompletion` call.
response = await copilotkit_stream(
await acompletion(
reasoning_effort="none",
allowed_openai_params=["reasoning_effort"],
model="openai/gpt-6-luna",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
*self.state.messages
],
stream=True
)
)
message = response.choices[0].messageHow is this guide?