langchain-ai/deepagents · error · RuntimeError

Device code request failed: HTTP {response.status_code} from

Error message

Device code request failed: HTTP {response.status_code} from {device_code_url}.

What it means

During the OAuth device-authorization flow, the initial POST to the provider's device_code endpoint returned an HTTP error status; _run_device_flow converts httpx.HTTPStatusError into a RuntimeError naming the status code and endpoint. The device flow never starts because the provider rejected the device-code request itself.

Source

Thrown at libs/code/deepagents_code/mcp_auth.py:1910

    init_data = {"client_id": client_id}
    if scope is not None:
        init_data["scope"] = scope

    async with httpx.AsyncClient(timeout=30.0) as client:
        response = await client.post(
            device_code_url,
            data=init_data,
            headers={"Accept": "application/json"},
        )
        try:
            response.raise_for_status()
        except httpx.HTTPStatusError as exc:
            msg = (
                f"Device code request failed: HTTP {response.status_code} "
                f"from {device_code_url}."
            )
            raise RuntimeError(msg) from exc
        try:
            device = _DeviceCodeResponse.model_validate(response.json())
        except (ValueError, ValidationError) as exc:
            msg = (
                f"Device code response from {device_code_url} is missing "
                f"required fields: {exc}"
            )
            raise RuntimeError(msg) from exc

        await interaction.show_device_code(
            verification_uri=device.verification_uri,
            user_code=device.user_code,
            expires_in=device.expires_in,
        )

        interval = max(device.interval, 1)
        loop = asyncio.get_running_loop()
        deadline = loop.time() + device.expires_in

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Check the HTTP status in the message: 401/404 usually mean a wrong client_id or device endpoint; 429 means rate limited - back off and retry.
  2. Verify the MCP server's OAuth metadata advertises the correct device_authorization_endpoint for your client_id.
  3. Confirm network/proxy access to the endpoint (curl the device_code_url) and retry `/mcp login <server>`.

Example fix

// before
device_code_url = "https://github.com/login/device/code2"  # typo
// after
device_code_url = "https://github.com/login/device/code"  # per provider docs
Defensive patterns

Strategy: retry

Validate before calling

# verify reachability and client_id before running the flow
import httpx
resp = httpx.post(device_code_url, data={"client_id": client_id})
resp.raise_for_status()  # surfaces the same 4xx/5xx before the library does

Try / catch

try:
    token = await _run_device_flow(...)
except RuntimeError as e:
    if str(e).startswith("Device code request failed"):
        if "HTTP 429" in str(e):
            await asyncio.sleep(30)  # rate limited: back off and retry
        else:
            raise SystemExit(f"Check client_id/device endpoint: {e}")
    else:
        raise

Prevention

When it happens

Trigger: _run_device_flow (and tests like test_device_code_request_failure_raises) POST to device_code_url and receive 4xx/5xx; also called via _preseed_github_auth in tests.

Common situations: Wrong or missing client_id for the provider, incorrect device authorization endpoint URL in the server's OAuth metadata, network/proxy returning 4xx/5xx, provider outage, or rate limiting.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/16e0fcb4fe26e5df. Report an issue: GitHub.