# LiteLLM (/zh-Hant/docs/guides/litellm)

從 LiteLLM Python SDK 呼叫 cheapaimodel 的模型，或透過 LiteLLM Proxy 提供這些模型。



LiteLLM 以 `openai/` 模型前綴連接 OpenAI 相容的服務。你可以直接在 Python 中使用，或把 cheapaimodel 的模型列進 LiteLLM Proxy 的設定檔。

|          | LiteLLM 設定 | 值                                                                       |
| -------- | ---------- | ----------------------------------------------------------------------- |
| Base URL | `api_base` | `https://api.cheapaimodel.ai/v1`                                        |
| API Key  | `api_key`  | 在控制台的 [API Key](https://console.cheapaimodel.ai/keys) 頁面建立的 Key         |
| 模型 ID    | `model`    | `openai/deepseek-v4-1-flash`：`openai/` 加上[目錄](/zh-Hant/models)中的任一模型 ID |

## 設定 [#設定]

<Tabs items="[&#x22;Python SDK&#x22;, &#x22;Proxy&#x22;]">
  <Tab value="Python SDK">
    ```bash title="Terminal"
    pip install litellm
    ```

    ```python title="app.py"
    import os
    import litellm

    response = litellm.completion(
        model="openai/deepseek-v4-1-flash",
        api_base="https://api.cheapaimodel.ai/v1",
        api_key=os.environ["CHEAPAIMODEL_API_KEY"],
        messages=[{"role": "user", "content": "Explain recursion in one sentence."}],
    )
    print(response.choices[0].message.content)

    # Streaming
    stream = litellm.completion(
        model="openai/deepseek-v4-1-flash",
        api_base="https://api.cheapaimodel.ai/v1",
        api_key=os.environ["CHEAPAIMODEL_API_KEY"],
        messages=[{"role": "user", "content": "Write a haiku about rivers."}],
        stream=True,
    )
    for chunk in stream:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
    ```

    LiteLLM 在送出前會去掉 `openai/` 前綴，因此請求以 `"model": "deepseek-v4-1-flash"` 送往 `/v1/chat/completions`。
  </Tab>

  <Tab value="Proxy">
    ```bash title="Terminal"
    pip install 'litellm[proxy]'
    ```

    ```yaml title="config.yaml"
    model_list:
      - model_name: deepseek-v4-1-flash
        litellm_params:
          model: openai/deepseek-v4-1-flash
          api_base: https://api.cheapaimodel.ai/v1
          api_key: os.environ/CHEAPAIMODEL_API_KEY

    general_settings:
      master_key: os.environ/LITELLM_MASTER_KEY
    ```

    每個模型加一個項目。`model_name` 是你的用戶端送給 Proxy 的名稱。目前的 LiteLLM 版本在沒有 master key 時會拒絕啟動 Proxy；用戶端以 master key 向 Proxy 驗證，Proxy 再以你的 cheapaimodel Key 呼叫 cheapaimodel。

    ```bash title="Terminal"
    export CHEAPAIMODEL_API_KEY="sk-..."
    export LITELLM_MASTER_KEY="sk-$(openssl rand -hex 32)"
    litellm --config config.yaml
    ```

    ```bash title="Terminal"
    curl http://localhost:4000/v1/chat/completions \
      -H "Authorization: Bearer $LITELLM_MASTER_KEY" \
      -H "Content-Type: application/json" \
      -d '{"model": "deepseek-v4-1-flash", "messages": [{"role": "user", "content": "Hello"}]}'
    ```
  </Tab>
</Tabs>

## 常見錯誤 [#常見錯誤]

* **`LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=deepseek-v4-1-flash`。** 漏了 `openai/` 前綴。LiteLLM 靠前綴選擇供應商，在送出任何請求前就會失敗。請寫成 `openai/deepseek-v4-1-flash`。
* **`litellm.NotFoundError` 或 `Invalid URL (POST /v1/chat/completions/chat/completions)`。** `api_base` 漏了 `/v1`，或寫入了端點路徑。請設為 `https://api.cheapaimodel.ai/v1`。
* **把 embeddings 指向 cheapaimodel。** cheapaimodel 目前沒有 embedding 模型，`litellm.embedding()` 或 `model_list` 中的 embedding 項目在這裡沒有模型可呼叫。請繼續使用其他供應商處理 embeddings。
* **LiteLLM 顯示這些呼叫沒有費用。** 它內建的價格表不包含 cheapaimodel 的模型：Proxy 記錄的費用為 0，`litellm.completion_cost()` 則會拋出 `This model isn't mapped yet`。實際花費請看控制台的[日誌](https://console.cheapaimodel.ai/logs)頁面。若要在 Proxy 中追蹤，請在該項目的 `model_info` 設定 `input_cost_per_token` 與 `output_cost_per_token`：取[模型頁面](/zh-Hant/models/deepseek-v4-1-flash)上每百萬 token 的價格除以 1,000,000。
