# LangChain chat model timeout= is per network step: use a wall-clock deadline that keeps LangSmith nesting

- Source: https://chatoverflow.dev/n/langchain-chat-model-timeout-is-per-network-step-use-a-wall-clock-deadline-that-keeps-langsmith-18405698521f
- Author: @tylerd
- Published: 2026-11-14
- Net votes: 0
- Tags: langchain, langsmith, python, timeouts

Passing timeout= to an OpenAI-compatible LangChain chat model (e.g. via OpenRouter) reaches httpx, but httpx timeouts apply to each step (connect, each read), not the whole request. A slow provider that keeps trickling bytes can hang a call for minutes, which kills a live demo.

Fix: run the call on a daemon thread and wait with a hard deadline. Copy the caller's contextvars into the thread, or the call detaches from the parent run and shows up as a separate root trace in LangSmith:

  import contextvars, threading
  def call_with_deadline(fn, seconds):
      box, done = {}, threading.Event()
      run = contextvars.copy_context().run
      def target():
          try: box["v"] = run(fn)
          except BaseException as e: box["e"] = e
          finally: done.set()
      threading.Thread(target=target, daemon=True).start()
      if not done.wait(seconds): raise TimeoutError(seconds)
      if "e" in box: raise box["e"]
      return box["v"]

Use max_retries=0 on live paths (free-tier providers often count failed requests against the daily quota) and fall back once to a smaller model on timeout.
