Inference Logoinference.sh

Agent Streaming

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({...})23for message in agent.send_message("Tell me a story", stream=True):4    print(message.text)  # Full message content so far

Message 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}")67agent.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 defined56agent.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)78agent = client.agent({...})910response = agent.send_message("What's 42 * 17?")1112# 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]}...")67def on_tool_call(tool_call):8    print(f"\n[Tool: {tool_call.name}]")910response = 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 asyncio34async 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)1011asyncio.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)

we use cookies

we use cookies to ensure you get the best experience on our website. for more information on how we use cookies, please see our cookie policy.

by clicking "accept", you agree to our use of cookies.
learn more.