# OpenAI Python SDK — API Store Hub

> VI: Hướng dẫn đổi base_url sang https://apistorehub.com/v1 trong OpenAI Python SDK (sync & AsyncOpenAI) để gọi Claude, GPT, Gemini và DeepSeek bằng một ví Credit ST.
> EN: Switch base_url to https://apistorehub.com/v1 in the official OpenAI Python SDK (sync and AsyncOpenAI) to call Claude, GPT, Gemini, and DeepSeek with one ST credit wallet.

- Canonical VI: https://apistorehub.com/vi/tich-hop/openai-python-sdk
- Canonical EN: https://apistorehub.com/en/tich-hop/openai-python-sdk
- Language: Python
- Runtime: Python 3.10+, openai >= 1.50, httpx
- Protocol: OpenAI Compatible (/v1/chat/completions, /v1/responses)
- Install: `pip install -U openai`
- Auth Env Var: `API_KEY` (prefix `sk-ash-`)

## Step-by-Step Setup
1. Install the official `openai` Python package (v1.50+) using pip or uv.
2. Export `API_KEY` using an API key prefixed with `sk-ash-` from the Dashboard.
3. Instantiate `OpenAI(base_url="https://apistorehub.com/v1", api_key=...)` or `AsyncOpenAI` for high concurrency.
4. Enable `stream=True` and configure `deepseek-v4-flash` as a secondary fallback model.

## Production Code Sample
```
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://apistorehub.com/v1",
    api_key=os.environ["API_KEY"],
    timeout=120.0,
    max_retries=2,
)

stream = client.chat.completions.create(
    model="gpt-6.2-sol",
    messages=[
        {"role": "system", "content": "Answer concisely with exact facts."},
        {"role": "user", "content": "Summarize 3 benefits of unified LLM routing."},
    ],
    stream=True,
)
for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
```

## Quick cURL Check
```bash
curl -N https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-6.2-sol","stream":true,"messages":[{"role":"user","content":"Hello"}]}'
```

## Recommended Models (Live ST Pricing)
| Model | Live Price | Context | Datasheet |
|---|---|---|---|
| `gpt-6.2-sol` | 6,700 ST / 1M tokens | 1.1M | https://apistorehub.com/llms/models/gpt-6-2-sol.md |
| `claude-sonnet-5-5` | 7,700 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/claude-sonnet-5-5.md |
| `deepseek-v4-flash` | 1,500 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/deepseek-v4-flash.md |

## FAQ
- **Do I need to rewrite payloads when migrating from OpenAI to API Store Hub?**: No. Update `base_url="https://apistorehub.com/v1"` and your `sk-ash-` API key; `messages`, `tools`, `response_format`, and `stream` stay unchanged.
- **Can OpenAI Python SDK call Claude and Gemini through API Store Hub?**: Yes. The unified `/v1/chat/completions` gateway routes seamlessly to Claude, GPT, Gemini, DeepSeek, Qwen, and Kimi from one ST wallet.
- **How can I track token usage during streaming?**: Pass `stream_options={"include_usage": True}` to receive exact prompt and completion token counts in the final SSE chunk.
