# 모델 및 가격 정책 (시스템 고급 API)

계정 권한 키(`tb_acct_...`)를 사용하여 현재 계정이 호출할 수 있는 모델 목록, 기본 과금 단가, 사이트/시간대별 할인, **현재 사용자 등급 할인 및 단일 모델 전용 할인**, 그리고 모델 가격/할인 변경 이력을 조회합니다.

> [!IMPORTANT]
> - **인증 및 보안 권장 사항**: 요청 헤더에 `Authorization: Bearer tb_acct_...` 또는 `X-Account-Api-Key: tb_acct_...`를 포함하세요. 키 보안을 강화하려면 계정에 2단계 인증(2FA)을 연동하는 것이 좋습니다.
> - **전용 할인 및 최저가 보호**: 현재 계정에 단일 모델 전용 할인이 설정된 경우 응답에 `user_model_discount`가 반환됩니다. 사이트 전체 최저가 제약에 걸리면 자동으로 최저가로 제한되며 `user_model_discount_limited`가 `true`로 설정됩니다.

---

### 1. 사용 가능한 모델 목록 및 가격 할인 조회

현재 사용자에게 표시되며 가용 채널이 지원하는 모델 목록을 조회합니다. 과금 단가(`billing`), 사이트 프로모션/시간대별 할인, 사용자 등급 할인, 사용자 단일 모델 전용 할인 및 최종 적용 할인이 포함되며, 키워드/카테고리 필터링과 최근 가격 조정 이력 인라인 반환을 지원합니다.

* **경로**: `GET /api/open/v1/models`
* **쿼리 매개변수 (Query)**:

| 매개변수명 | 타입 | 기본값 | 설명 |
| :--- | :--- | :--- | :--- |
| `keyword` | `string` | 없음 | `model_id`, `mid`, `display_name` 또는 `original_id`로 퍼지 필터링 |
| `category` | `string` | 전체 | 모델 카테고리로 정확히 필터링 (예: `chat`, `image`, `video`, `audio`) |
| `include_price_history` | `integer` | `0` | `1`을 전달하면 각 모델에 최근 가격 조정 이력 배열 `price_history`를 함께 반환 |
| `history_limit` | `integer` | `10` | `include_price_history=1`일 때 모델당 반환할 최대 변동 건수 (범위 `1–50`) |

* **호출 예시 (Curl)**:

```bash
curl -s "https://www.tokenpad.ai/api/open/v1/models?keyword=gpt&include_price_history=1&history_limit=5" \
  -H "Authorization: Bearer tb_acct_your_account_key"
```

* **응답 예시**:

```json
{
  "data": [
    {
      "id": 12,
      "mid": "m_gpt4o",
      "model_id": "gpt-4o",
      "original_id": "gpt-4o-2024-11-20",
      "display_name": "GPT-4o",
      "category": "chat",
      "provider": "OpenAI",
      "billing_invalid": false,
      "billing": {
        "billing_type": "tokens",
        "billing_rule": "token_tiered",
        "prompt_rate": 2.5,
        "completion_rate": 10.0,
        "cached_rate": 1.25,
        "claude_cache_creation_rate": 0.0,
        "claude_cache_read_rate": 0.0,
        "fixed_rate": 0.0,
        "duration_rate": 0.0,
        "pricing_tiers": null,
        "extended_config": {}
      },
      "discounts": {
        "global_discount_enabled": 1,
        "global_discount": 0.85,
        "base_global_discount": null,
        "discount_schedule": null,
        "resolution_discounts": null,
        "user_level_discount": 0.9,
        "user_model_discount": 0.7,
        "user_model_discount_limited": false,
        "effective_discount": 0.7
      },
      "price_history": [
        {
          "id": 108,
          "model_id": 12,
          "mid": "m_gpt4o",
          "model_key": "gpt-4o",
          "original_id": "gpt-4o-2024-11-20",
          "model_name": "GPT-4o",
          "provider_name": "OpenAI",
          "type_name": "chat",
          "change_type": "discount",
          "change_summary": "사이트 전체 할인 조정: 1.0 -> 0.85",
          "previous_discount": 1.0,
          "current_discount": 0.85,
          "previous_snapshot": { "effective_discount": 1.0 },
          "current_snapshot": { "effective_discount": 0.85 },
          "created_at": "2026-11-12 12:00:00 (UTC+8)"
        }
      ]
    }
  ],
  "total": 1
}
```

* **`discounts` 핵심 필드 설명**:

| 필드명 | 설명 |
| :--- | :--- |
| `global_discount_enabled` / `global_discount` | 사이트 할인 활성화 여부(`1`/`0`) 및 현재 적용 중인 사이트 할인 계수 (예: `0.85`는 15% 할인을 의미) |
| `base_global_discount` | 시간대별 할인 계획(`discount_schedule`)이 적용된 경우 시간대별 할인 적용 전의 기본 사이트 할인을 반환 |
| `user_level_discount` | 현재 계정이 속한 사용자 등급의 기본 할인 계수 |
| `user_model_discount` | 관리자가 현재 사용자에게 해당 모델에 대해 개별 설정한 전용 할인 (미설정 시 `null`) |
| `user_model_discount_limited` | `true`인 경우 기존 전용 할인이 모델 최저가 보호 제약에 의해 최저가로 자동 상향 조정되었음을 의미 |
| `effective_discount` | 계정 할인 정책과 최저가 규칙을 결합하여 계산된 현재 실제 최소 적용 할인 |

---

### 2. 모델 가격 및 할인 변동 이력 조회 (일괄 / 필터링)

현재 사용자에게 표시되는 모델의 가격 또는 할인 변경 이력을 조회합니다(초기 기준선 레코드는 필터링됨).

* **경로**: `GET /api/open/v1/models/price-history`
* **쿼리 매개변수 (Query)**:

| 매개변수명 | 타입 | 기본값 | 설명 |
| :--- | :--- | :--- | :--- |
| `model_id` | `string` | 없음 | 단일 모델의 숫자 `id`, `mid`, `model_id`, `original_id` 또는 표시 이름으로 필터링 |
| `ids` | `string` | 없음 | 쉼표로 구분된 여러 모델의 숫자 `id`로 일괄 필터링 (예: `12,18,25`), `model_id`보다 우선순위가 높음 |
| `change_type` | `string` | `all` | 변경 유형 필터링 (예: `price`, `discount`, 비워 두거나 `all`이면 초기 외 모든 변경 반환) |
| `limit` | `integer` | `50` | 반환할 최대 건수 (범위 `1–200`, 최신순 정렬) |

* **호출 예시 (Curl)**:

```bash
curl -s "https://www.tokenpad.ai/api/open/v1/models/price-history?model_id=gpt-4o&limit=20" \
  -H "Authorization: Bearer tb_acct_your_account_key"
```

---

### 3. 단일 모델 가격 변동 이력 조회

경로 매개변수로 지정한 모델의 과거 가격/할인 변경 내역을 빠르게 조회합니다.

* **경로**: `GET /api/open/v1/models/{id}/price-history`
* **경로 매개변수**: `id`는 모델의 숫자 `id`, `mid`(예: `m_gpt4o`), `model_id`(예: `gpt-4o`) 또는 `original_id`를 지원합니다
* **쿼리 매개변수 (Query)**: `change_type` 및 `limit` 지원 (범위 `1–200`, 기본값 `50`)
* **호출 예시 (Curl)**:

```bash
curl -s "https://www.tokenpad.ai/api/open/v1/models/gpt-4o/price-history?limit=10" \
  -H "Authorization: Bearer tb_acct_your_account_key"
```

* **응답 예시**:

```json
{
  "items": [
    {
      "id": 108,
      "model_id": 12,
      "mid": "m_gpt4o",
      "model_key": "gpt-4o",
      "original_id": "gpt-4o-2024-11-20",
      "model_name": "GPT-4o",
      "provider_name": "OpenAI",
      "type_name": "chat",
      "change_type": "discount",
      "change_summary": "사이트 전체 할인 조정: 1.0 -> 0.85",
      "previous_discount": 1.0,
      "current_discount": 0.85,
      "previous_snapshot": { "effective_discount": 1.0 },
      "current_snapshot": { "effective_discount": 0.85 },
      "created_at": "2026-11-12 12:00:00 (UTC+8)"
    }
  ],
  "total": 1
}
```
