{
  "openapi": "3.1.0",
  "info": {
    "title": "Pixizen Text Chat API",
    "version": "1.0.0",
    "description": "Text-only LLM chat, streaming and project-scoped realtime usage events. Replace the server URL with your deployed Pixizen backend. Secret API keys must stay on customer servers."
  },
  "servers": [
    {
      "url": "https://api.example.com/api/chat/v1",
      "description": "Replace with your actual Pixizen backend origin"
    }
  ],
  "security": [
    {
      "ApiKey": []
    }
  ],
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "Project-scoped px_live_ API key"
      }
    },
    "schemas": {
      "Message": {
        "type": "object",
        "required": [
          "role",
          "content"
        ],
        "properties": {
          "role": {
            "type": "string",
            "enum": [
              "system",
              "user",
              "assistant"
            ]
          },
          "content": {
            "type": "string",
            "minLength": 1,
            "maxLength": 32000
          }
        }
      },
      "Usage": {
        "type": "object",
        "properties": {
          "prompt_tokens": {
            "type": "integer"
          },
          "completion_tokens": {
            "type": "integer"
          },
          "total_tokens": {
            "type": "integer"
          },
          "prompt_tokens_details": {
            "type": "object",
            "properties": {
              "cached_tokens": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "cache_metering_available": {
                "type": "boolean"
              }
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            }
          }
        }
      },
      "Completion": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "object": {
            "const": "chat.completion"
          },
          "model": {
            "type": "string"
          },
          "choices": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "index": {
                  "type": "integer"
                },
                "message": {
                  "$ref": "#/components/schemas/Message"
                },
                "finish_reason": {
                  "const": "stop"
                }
              }
            }
          },
          "usage": {
            "$ref": "#/components/schemas/Usage"
          },
          "cost_usd": {
            "type": "number"
          }
        }
      }
    }
  },
  "paths": {
    "/models": {
      "get": {
        "operationId": "listModels",
        "summary": "Discover the configured text chat model",
        "responses": {
          "200": {
            "description": "Model list"
          },
          "401": {
            "description": "Invalid or revoked key"
          }
        }
      }
    },
    "/chat/completions": {
      "post": {
        "operationId": "createChatCompletion",
        "summary": "Generate a text reply or stream it using SSE",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 120
            },
            "description": "Unique logical request identifier. Matching completed requests replay without another charge. Mismatch or unresolved request returns 409."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "model",
                  "messages"
                ],
                "properties": {
                  "model": {
                    "type": "string",
                    "examples": [
                      "pixil-xd-1.3"
                    ]
                  },
                  "messages": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 40,
                    "items": {
                      "$ref": "#/components/schemas/Message"
                    }
                  },
                  "max_tokens": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "Maximum generated tokens; bounded by published model configuration."
                  },
                  "stream": {
                    "type": "boolean",
                    "default": false
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON completion, or SSE data chunks followed by a completed event and [DONE]. SSE can terminate with an error event after partial output.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Completion"
                }
              },
              "text/event-stream": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "400": {
            "description": "Invalid text-only request or input limit exceeded"
          },
          "401": {
            "description": "Invalid or expired key"
          },
          "402": {
            "description": "Insufficient available balance or key budget"
          },
          "403": {
            "description": "Suspended account/project"
          },
          "409": {
            "description": "Idempotency conflict or unresolved request"
          },
          "429": {
            "description": "RPM, reserved TPM, or concurrency limit"
          },
          "502": {
            "description": "Provider response/usage unavailable; reservation may require reconciliation"
          },
          "503": {
            "description": "Pricing or provider not configured"
          }
        }
      }
    },
    "/events": {
      "get": {
        "operationId": "subscribeEvents",
        "summary": "Subscribe to durable project-scoped usage events",
        "description": "SSE events usage.completed and usage.reconciliation. Numeric sequence cursor; seven-day retention. Reconnect using Last-Event-ID. Credentials are revalidated at least every 15 seconds. Five streams per API key.",
        "parameters": [
          {
            "name": "Last-Event-ID",
            "in": "header",
            "schema": {
              "type": "string",
              "pattern": "^[0-9]{1,15}$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "SSE stream with id, event and data fields plus heartbeats",
            "content": {
              "text/event-stream": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "401": {
            "description": "Key expired/revoked"
          },
          "429": {
            "description": "Too many open streams"
          }
        }
      }
    }
  }
}
