Use your existing ChatGPT Plus or Pro subscription to access OpenAI’s Codex models without consuming API credits. The SDK handles OAuth authentication, credential caching, and automatic token refresh.
"""Example: Using ChatGPT subscription for Codex models.This example demonstrates how to use your ChatGPT Plus/Pro subscriptionto access OpenAI's Codex models without consuming API credits.The subscription_login() method handles:- OAuth PKCE authentication flow- Credential caching (~/.local/share/openhands/auth/)- Automatic token refreshSupported models:- gpt-5.2-codex (default)- gpt-5.2- gpt-5.1-codex-max- gpt-5.1-codex-miniRequirements:- Active ChatGPT Plus or Pro subscription- Browser access for initial OAuth login"""import osfrom openhands.sdk import LLM, Agent, Conversation, Toolfrom openhands.tools.file_editor import FileEditorToolfrom openhands.tools.terminal import TerminalTool# First time: Opens browser for OAuth login# Subsequent calls: Reuses cached credentials (auto-refreshes if expired)llm = LLM.subscription_login( model="gpt-5.2-codex", # or "gpt-5.2", "gpt-5.1-codex-max", "gpt-5.1-codex-mini")# Verify subscription mode is activeprint(f"Using subscription mode: {llm.is_subscription}")# Use the LLM with an agent as usualagent = Agent( llm=llm, tools=[ Tool(name=TerminalTool.name), Tool(name=FileEditorTool.name), ],)cwd = os.getcwd()conversation = Conversation(agent=agent, workspace=cwd)conversation.send_message("List the files in the current directory.")conversation.run()print("Done!")# Alternative: Force a fresh login (useful if credentials are stale)# llm = LLM.subscription_login(model="gpt-5.2-codex", force_login=True)# Alternative: Disable auto-opening browser (prints URL to console instead)# llm = LLM.subscription_login(model="gpt-5.2-codex", open_browser=False)
Running the Example
Copy
Ask AI
cd agent-sdkuv run python examples/01_standalone_sdk/34_subscription_login.py