Handle real-time message updates and tool calls.
Note: Agent streaming sends the full message content on each update, not incremental chunks. You don't need to concatenate—each update gives you the complete message so far.
Basic Streaming
1agent = client.agent({...})2 3for message in agent.send_message("Tell me a story", stream=True):4 print(message.text) # Full message content so farMessage Callbacks
Handle streaming with callbacks:
1def on_message(text, done):2 if done:3 print(f"\n[Complete] Final: {text}")4 else:5 print(f"Current message: {text}")6 7agent.send_message(8 "Tell me a story",9 on_message=on_message10)Tool Call Callbacks
Handle tool invocations:
1def on_tool_call(tool_call):2 print(f"Calling: {tool_call.name}")3 print(f"Args: {tool_call.arguments}")4 # Tool is executed automatically if handler is defined5 6agent.send_message(7 "What's 42 * 17?",8 on_tool_call=on_tool_call9)Manual Tool Handling
For tools without handlers:
1calculator = (2 tool("calculator")3 .describe("Do math")4 .param("expression", string("Expression"))5 .build() # No handler6)7 8agent = client.agent({...})9 10response = agent.send_message("What's 42 * 17?")11 12# Check for pending tool calls13if response.pending_tool_calls:14 for tc in response.pending_tool_calls:15 result = eval(tc.arguments["expression"])16 agent.submit_tool_result(tc.id, str(result))Combined Callbacks
1def on_message(text, done):2 if done:3 print(f"\nFinal: {text}")4 else:5 print(f"Progress: {text[:50]}...")6 7def on_tool_call(tool_call):8 print(f"\n[Tool: {tool_call.name}]")9 10response = agent.send_message(11 "Generate an image of a sunset",12 on_message=on_message,13 on_tool_call=on_tool_call14)Async Client
1from inferencesh import AsyncInference2import asyncio3 4async def main():5 client = AsyncInference(api_key="inf_...")6 agent = client.agent({...})7 8 for message in agent.send_message("Hello", stream=True):9 print(message.text)10 11asyncio.run(main())Send Options
1# Python kwargs2agent.send_message(3 message="Hello",4 stream=True,5 on_message=lambda text, done: print(text),6 on_tool_call=lambda tc: print(tc.name)7)