> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agno.com/llms.txt
> Use this file to discover all available pages before exploring further.

# xAI SuperGrok Device Login

> Sign in with a SuperGrok subscription instead of an API key and run an agent through xAIResponses.

Sign in with a SuperGrok subscription instead of an API key and run an agent through xAIResponses. The device flow prints a URL and a code; approve the sign-in in the browser and the token is stored encrypted and refreshed automatically across restarts.

```python oauth_device_login.py theme={null}
"""
Xai SuperGrok Device Login
==========================

Sign in with a SuperGrok subscription instead of an API key and run an agent
through xAIResponses. The device flow prints a URL and a code; approve the
sign-in in the browser and the token is stored encrypted and refreshed
automatically across restarts.

Requires XAI_TOKEN_ENCRYPTION_KEY. Generate a key with:
python -c "from agno.utils.encryption import generate_encryption_key; print(generate_encryption_key())"
"""

import time

from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.xai import xAIResponses
from agno.models.xai.oauth import XAITokenManager

# SqliteDb is for local development only; use PostgresDb in production
db = SqliteDb(db_file="tmp/xai_oauth.db")
token_manager = XAITokenManager(db=db)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    # --- Sign in with the device flow ---
    info = token_manager.start_device_login()
    print("Open this URL and approve the sign-in:")
    print(info.verification_uri_complete)
    print("Code: " + info.user_code)
    token_manager.poll_for_token(
        info.device_code, info.interval, time.time() + info.expires_in
    )
    print("Signed in. The token is stored encrypted and refreshes automatically.")

    # --- Model class syntax ---
    agent = Agent(model=xAIResponses(token_manager=token_manager), markdown=True)
    agent.print_response("Share a 2 sentence horror story")

    # --- String syntax ---
    agent = Agent(model="xai-responses:grok-4.3", markdown=True)
    # Attach the SuperGrok session to the model the string resolved to
    agent.model.token_manager = token_manager
    agent.print_response("Share a 2 sentence horror story")
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno openai sqlalchemy
    ```
  </Step>

  <Step title="Export your xAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export XAI_API_KEY="your_xai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:XAI_API_KEY="your_xai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Save the code above as `oauth_device_login.py`, then run:

    ```bash theme={null}
    python oauth_device_login.py
    ```
  </Step>
</Steps>

Full source: [cookbook/90\_models/xai/oauth\_device\_login.py](https://github.com/agno-agi/agno/blob/v3.0.4/cookbook/90_models/xai/oauth_device_login.py)
