Register and share your invite link to earn from video plays and referrals.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture 投稿は個人の意見です。
Joined May 2026
280 Following    415 Followers
# Useful but Little-Known Features of OpenAI Agent SDK 🌍 Did you know you can stop a streaming agent run mid-generation with a single method call? `RunResultStreaming.cancel()` lets you safely cancel a streaming run on demand. 📌 Title: RunResultStreaming.cancel() 🔗 URL: 🧩 Overview The `cancel()` method on `RunResultStreaming` stops a streaming agent run either immediately or after the current turn completes. After calling `cancel()`, you must continue consuming the `stream_events()` async iterator so that cancellation and cleanup finish correctly. The `is_complete` property indicates whether the run reached its terminal state, and summary properties like `final_output` and `interruptions` finalize after the last token. 🛠 How to use it ```python from agents import Agent, Runner agent = Agent(name="Assistant", instructions="You are helpful.") result = "Write a long essay...") async for event in # User pressed the cancel button if user_cancelled(): result.cancel() # Keep consuming for cleanup continue # Normal event processing handle_event(event) # Check state after cleanup completes print(f"Complete: {") ``` 🏗 Building it into production ・Wire a "Stop" button in your chat UI to trigger user-initiated cancellation ・Always consume `stream_events()` to completion after cancel to prevent resource leaks ・Check `is_complete` to distinguish cancelled runs from normal completions in your logs ・Use cancellation for cost management by cutting off unnecessary token generation early 💡 Use cases 🛑 Implementing a "Stop generating" button in chatbots 💰 Reducing token costs on overly long responses ⏱ Timeout-based automatic cancellation 🔄 Aborting a previous run when the user changes their question ⚠️ Watch out Skipping `stream_events()` consumption after `cancel()` prevents proper resource cleanup. Always exhaust the iterator. Depending on timing, the current turn may complete before the cancellation takes effect. ✨ With cancel(), give users full control over responsive streaming experiences. #OpenAIAgentSDK# #AIAgent#
Show more