﻿# A device-code login poller needs grant-specific pending and stop states

An enterprise connector must distinguish pending device authorization, slower polling and a finished grant rather than feeding every token response into a generic retry loop.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/enterprise-ai-device-grant-polling-state-lifetime/
Language: en

## Model a grant rather than an endless login loop

An enterprise connector running without a suitable local browser asks its operator to complete device authorization elsewhere. The operator is still reading the prompt when the token endpoint returns an error-shaped pending response. A generic retry handler either reports a failed login immediately or polls as fast as possible. Both outcomes ignore the grant’s interaction contract. Represent the active authorization session, its expiry and its next permissible poll as explicit state.

## Distinguish the codes given to user and client

The [Microsoft flow documentation](https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-device-code) distinguishes the device code used by the client from the user code and verification instructions shown to the operator. It also returns expiry and polling information and recommends starting when the user is ready. Preserve the association instead of creating a fresh authorization session every time a status page refreshes. Keep device codes out of ordinary conversation transcripts and shared operational logs.

## Apply persistent polling changes

The [device-grant standard](https://www.rfc-editor.org/rfc/rfc8628.html) permits continued polling for authorization_pending under its interval rules. slow_down requires increasing the interval by 5 seconds for that and subsequent requests. If no interval was provided, the standard gives a 5-second baseline. Keep the effective interval as grant state, so a later pending response or worker restart does not silently restore the earlier faster schedule. A successful HTTP exchange does not itself mean authorization was granted.

## Stop the concluded authorization session

The standard treats access_denied and expired_token as terminal for polling that grant; errors other than its pending cases also require stopping and appropriate handling. A connection timeout is a different observation and requires reduced polling frequency before retry. Do not reset the grant merely to hide these distinctions. After expiration, a new authorization request should normally wait for user interaction instead of continuously issuing replacement codes that make the operator’s displayed instructions obsolete.

## Test time and terminal transitions

Use a fake token endpoint and a controllable clock to return pending, slow_down, another pending response and then expiry. Check the retained interval across restart and verify that terminal outcomes stop polling. Separately inject a connection timeout and an operator-requested fresh attempt. Acceptance requires requests to follow the active grant’s schedule and the display to identify the current session. No tenant authorization or token-endpoint polling was performed here; no authentication throughput is claimed.

## Do not turn login into tool approval

[PKCE browser login](/journal/learn-openrouter-oauth-pkce/) covers another authentication flow. [Client-history trust](/journal/guide-pydantic-ai-client-history-trust/) separates application authority from caller-supplied claims. Receiving a token completes a protocol step under the granted scope, not approval of every proposed AI action. Keep account identity, intended resource and current business authorization visible after login, and use a supported authentication library when available instead of maintaining bespoke protocol machinery without a clear integration need.
