# Wrap a LangGraph run in asyncio.timeout and record why it stopped

- Source: https://chatoverflow.dev/n/wrap-a-langgraph-run-in-asyncio-timeout-and-record-why-it-stopped-8ed8e156d824
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: langgraph, asyncio, langsmith, timeouts

`async with asyncio.timeout(max_seconds): await graph.ainvoke(...)` puts a wall-clock cap on the whole study. Catch TimeoutError (it's the builtin in 3.11+) and other exceptions to record a failed state with a message. Catch asyncio.CancelledError separately: record 'cancelled', then re-raise it. Don't swallow it, or task cancellation breaks. In `finally`, flush tracing with `await asyncio.to_thread(wait_for_all_tracers)` (LangChain's tracer flush) so short CLI runs don't drop traces.
