{
  "openapi": "3.1.0",
  "info": {
    "title": "Premedice API",
    "version": "2026-07-24.1",
    "description": "OpenAI-compatible medical AI API. One public model ID, Premedice/Premed-4, routes internally to low, mid, or high tiers based on the caller's Premedice package. Medical tools run server-side. MCP is exposed at /mcp.",
    "contact": {
      "name": "Premedice",
      "url": "https://premedice.com"
    }
  },
  "servers": [
    {
      "url": "https://api.premedice.com",
      "description": "Production"
    },
    {
      "url": "http://localhost:3000",
      "description": "Local development"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "summary": "Liveness check",
        "security": [],
        "responses": {
          "200": {
            "description": "Process is serving",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Health"
                }
              }
            }
          }
        }
      }
    },
    "/health/ready": {
      "get": {
        "operationId": "getHealthReady",
        "summary": "Readiness check",
        "security": [],
        "responses": {
          "200": {
            "description": "Process is ready",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ready": { "type": "boolean" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/health/live": {
      "get": {
        "operationId": "getHealthLive",
        "summary": "Liveness probe",
        "security": [],
        "responses": {
          "200": {
            "description": "Process is alive",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "live": { "type": "boolean" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/models": {
      "get": {
        "operationId": "listModels",
        "summary": "List available models",
        "security": [],
        "responses": {
          "200": {
            "description": "OpenAI-compatible model list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "object": { "type": "string", "const": "list" },
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Model"
                      }
                    }
                  },
                  "required": ["object", "data"]
                }
              }
            }
          }
        }
      }
    },
    "/v1/chat/completions": {
      "post": {
        "operationId": "createChatCompletion",
        "summary": "Create a chat completion",
        "description": "OpenAI-compatible chat completions. Streaming is the default. Medical tools are preloaded server-side. Non-medical tools supplied by the caller are returned as tool_calls for local execution rather than being run by this service.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChatCompletionRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Chat completion, as JSON or Server-Sent Events",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ChatCompletion"
                }
              },
              "text/event-stream": {
                "schema": {
                  "type": "string",
                  "description": "OpenAI chat.completion.chunk events followed by data: [DONE]"
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/PaymentRequired" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "413": { "$ref": "#/components/responses/PayloadTooLarge" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "502": { "$ref": "#/components/responses/UpstreamError" }
        }
      }
    },
    "/v1/labs/interpret": {
      "post": {
        "operationId": "interpretLabs",
        "summary": "Interpret a structured lab panel",
        "description": "Validates 1-80 lab results, runs the shared medical pipeline, and returns an OpenAI-shaped completion with a premedice block. Billed like chat. A deterministic rule-based summary is returned when the model produces no content.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LabInterpretRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Lab interpretation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/LabInterpretResponse"
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/PaymentRequired" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "413": { "$ref": "#/components/responses/PayloadTooLarge" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "502": { "$ref": "#/components/responses/UpstreamError" }
        }
      }
    },
    "/v1/evidence/verify": {
      "post": {
        "operationId": "verifyEvidence",
        "summary": "Verify a clinical claim against supplied sources",
        "description": "Checks a claim only against the sources supplied in the request. Free in v1. Fails open with checked=false when the verifier is unavailable, times out, or returns unparseable output. Sources without text force low confidence.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/EvidenceVerifyRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Verification result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EvidenceVerifyResponse"
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/PaymentRequired" },
          "413": { "$ref": "#/components/responses/PayloadTooLarge" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/mcp": {
      "post": {
        "operationId": "mcpJsonRpc",
        "summary": "MCP JSON-RPC 2.0 endpoint",
        "description": "Model Context Protocol server over HTTP. Supported methods: initialize, notifications/initialized, ping, tools/list, tools/call, prompts/list, prompts/get. Exposes 26 tools (23 medical tools plus premedice_chat, lab_interpret, evidence_verify) and the medical_research prompt. Only tools/call counts against the rate limit; handshakes and listings do not.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/JsonRpcRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/JsonRpcResponse"
                }
              }
            }
          },
          "204": {
            "description": "No content for notifications"
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/PaymentRequired" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Premedice API key. Keys start with pm_live_ or pm_test_."
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Invalid JSON, missing messages, or failed validation",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "Unauthorized": {
        "description": "Missing, malformed, or revoked API key",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "PaymentRequired": {
        "description": "Paid subscription required or insufficient credits",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "Forbidden": {
        "description": "Requested tier is not available on this package",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "PayloadTooLarge": {
        "description": "Request body exceeds 1 MB",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "RateLimited": {
        "description": "Rate limit exceeded. The response body carries the retry hint; no Retry-After header is set.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "UpstreamError": {
        "description": "Upstream model or research service failed",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "message": { "type": "string" },
              "type": { "type": "string", "examples": ["premedice_error", "rate_limit_error"] },
              "code": { "type": "integer" },
              "request_id": { "type": "string" }
            },
            "required": ["message", "type", "code"]
          }
        },
        "required": ["error"]
      },
      "Health": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "examples": ["ok"] },
          "version": { "type": "string" },
          "uptime": { "type": "number" },
          "now": { "type": "string", "format": "date-time" }
        },
        "required": ["status", "version", "uptime", "now"]
      },
      "Model": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "const": "Premedice/Premed-4" },
          "object": { "type": "string", "const": "model" },
          "created": { "type": "integer" },
          "owned_by": { "type": "string", "const": "premedice" },
          "permission": { "type": "array", "items": {} }
        },
        "required": ["id", "object", "created", "owned_by"]
      },
      "Message": {
        "type": "object",
        "properties": {
          "role": { "type": "string", "enum": ["system", "user", "assistant", "tool"] },
          "content": { "type": ["string", "null"] },
          "name": { "type": "string" },
          "tool_call_id": { "type": "string" },
          "tool_calls": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ToolCall" }
          }
        },
        "required": ["role", "content"]
      },
      "ToolCall": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "type": { "type": "string", "const": "function" },
          "function": {
            "type": "object",
            "properties": {
              "name": { "type": "string" },
              "arguments": { "type": "string", "description": "JSON-encoded arguments" }
            },
            "required": ["name", "arguments"]
          }
        },
        "required": ["id", "type", "function"]
      },
      "PatientContext": {
        "type": "object",
        "properties": {
          "age": { "type": "number" },
          "sex": { "type": "string", "enum": ["male", "female", "other"] },
          "weight_kg": { "type": "number" },
          "height_cm": { "type": "number" },
          "conditions": { "type": "array", "items": { "type": "string" } },
          "medications": { "type": "array", "items": { "type": "string" } },
          "allergies": { "type": "array", "items": { "type": "string" } },
          "surgical_history": { "type": "array", "items": { "type": "string" } },
          "family_history": { "type": "array", "items": { "type": "string" } },
          "social_history": { "type": "string" },
          "lab_values": { "type": "object", "additionalProperties": { "type": "number" } },
          "encounter_type": { "type": "string", "enum": ["emergency", "inpatient", "outpatient", "telehealth"] },
          "chief_complaint": { "type": "string" }
        }
      },
      "PremediceParams": {
        "type": "object",
        "properties": {
          "audience": { "type": "string", "enum": ["clinician", "patient", "student", "researcher"] },
          "format": { "type": "string", "enum": ["concise", "detailed", "structured", "patient_friendly"] },
          "citation_style": { "type": "string", "enum": ["inline", "numbered", "footnote", "hidden"] },
          "output_format": {
            "type": "string",
            "enum": ["auto", "differential", "medication_review", "assessment_plan", "soap", "evidence_summary", "patient_education", "code_lookup"]
          },
          "reasoning_visibility": { "type": "string", "enum": ["none", "brief", "full"] },
          "language": { "type": "string" },
          "safety_checks": { "type": "boolean" },
          "evidence_grading": { "type": "boolean" },
          "max_tool_rounds": { "type": ["integer", "null"] },
          "tool_budget_tokens": { "type": "integer" },
          "tool_budget_ms": { "type": "integer" },
          "conversation_id": { "type": ["string", "null"] },
          "skip_safety": { "type": "boolean" }
        }
      },
      "ChatCompletionRequest": {
        "type": "object",
        "properties": {
          "model": {
            "type": "string",
            "default": "Premedice/Premed-4",
            "description": "Public model ID. Omit or pass Premedice/Premed-4 to auto-route by package; low, mid, and high force a tier when the package allows it."
          },
          "messages": {
            "type": "array",
            "minItems": 1,
            "items": { "$ref": "#/components/schemas/Message" }
          },
          "stream": {
            "type": "boolean",
            "default": true,
            "description": "Streaming is the default. Set false for one JSON response."
          },
          "temperature": { "type": "number", "minimum": 0, "maximum": 2, "default": 0.3 },
          "max_tokens": { "type": "integer", "minimum": 1, "maximum": 4096, "default": 4096 },
          "top_p": { "type": "number", "minimum": 0, "maximum": 1 },
          "stop": { "oneOf": [{ "type": "string" }, { "type": "array", "items": { "type": "string" } }] },
          "user": { "type": "string" },
          "tools": {
            "type": "array",
            "description": "Optional client tools. Names that match a built-in medical tool run server-side; other names are returned as tool_calls for local execution.",
            "items": { "type": "object" }
          },
          "tool_choice": {
            "oneOf": [
              { "type": "string", "enum": ["auto", "none"] },
              {
                "type": "object",
                "properties": {
                  "type": { "type": "string", "const": "function" },
                  "function": {
                    "type": "object",
                    "properties": { "name": { "type": "string" } },
                    "required": ["name"]
                  }
                },
                "required": ["type", "function"]
              }
            ]
          },
          "patient": { "$ref": "#/components/schemas/PatientContext" },
          "premedice": { "$ref": "#/components/schemas/PremediceParams" }
        },
        "required": ["messages"]
      },
      "Usage": {
        "type": "object",
        "properties": {
          "prompt_tokens": { "type": "integer" },
          "completion_tokens": { "type": "integer" },
          "total_tokens": { "type": "integer" }
        },
        "required": ["prompt_tokens", "completion_tokens", "total_tokens"]
      },
      "ChatCompletion": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "object": { "type": "string", "const": "chat.completion" },
          "created": { "type": "integer" },
          "model": { "type": "string", "const": "Premedice/Premed-4" },
          "choices": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "index": { "type": "integer" },
                "finish_reason": { "type": ["string", "null"] },
                "message": {
                  "type": "object",
                  "properties": {
                    "role": { "type": "string", "const": "assistant" },
                    "content": { "type": ["string", "null"] },
                    "reasoning": { "type": ["string", "null"] },
                    "tool_calls": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/ToolCall" }
                    }
                  },
                  "required": ["role", "content"]
                }
              },
              "required": ["index", "finish_reason", "message"]
            }
          },
          "usage": { "$ref": "#/components/schemas/Usage" },
          "premedice_meta": {
            "type": "object",
            "properties": {
              "modes": { "type": "array", "items": { "type": "string" } },
              "query_hints": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "category": { "type": "string" },
                    "message": { "type": "string" }
                  }
                }
              },
              "safety_flags": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "type": { "type": "string" },
                    "severity": { "type": "string" },
                    "message": { "type": "string" }
                  }
                }
              },
              "sources": {
                "type": "array",
                "items": { "$ref": "#/components/schemas/Source" }
              },
              "tools_used": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "name": { "type": "string" },
                    "ms": { "type": "number" },
                    "ok": { "type": "boolean" },
                    "error": { "type": "string" }
                  }
                }
              }
            }
          }
        },
        "required": ["id", "object", "created", "model", "choices", "usage"]
      },
      "Source": {
        "type": "object",
        "properties": {
          "type": { "type": "string" },
          "id": { "type": "string" },
          "title": { "type": "string" },
          "url": { "type": "string" },
          "extra": { "type": "object" }
        },
        "required": ["type", "id"]
      },
      "LabEntry": {
        "type": "object",
        "properties": {
          "analyte": { "type": "string", "maxLength": 80 },
          "value": { "oneOf": [{ "type": "number" }, { "type": "string", "maxLength": 40 }] },
          "unit": { "type": "string", "maxLength": 30 },
          "reference_low": { "type": "number" },
          "reference_high": { "type": "number" },
          "flag": { "type": "string", "maxLength": 40 },
          "collected_at": { "type": "string", "maxLength": 40 }
        },
        "required": ["analyte"]
      },
      "LabInterpretRequest": {
        "type": "object",
        "properties": {
          "labs": {
            "type": "array",
            "minItems": 1,
            "maxItems": 80,
            "items": { "$ref": "#/components/schemas/LabEntry" }
          },
          "question": { "type": "string", "maxLength": 4000 },
          "patient_context": { "$ref": "#/components/schemas/PatientContext" },
          "variant": { "type": "string", "enum": ["low", "mid", "high"] }
        },
        "required": ["labs"]
      },
      "LabInterpretResponse": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "object": { "type": "string", "const": "chat.completion" },
          "created": { "type": "integer" },
          "model": { "type": "string", "const": "Premedice/Premed-4" },
          "choices": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "index": { "type": "integer" },
                "finish_reason": { "type": "string" },
                "message": {
                  "type": "object",
                  "properties": {
                    "role": { "type": "string", "const": "assistant" },
                    "content": { "type": "string" }
                  },
                  "required": ["role", "content"]
                }
              },
              "required": ["index", "finish_reason", "message"]
            }
          },
          "usage": { "$ref": "#/components/schemas/Usage" },
          "premedice": {
            "type": "object",
            "properties": {
              "sources": { "type": "array", "items": { "$ref": "#/components/schemas/Source" } },
              "safety_flags": { "type": "array", "items": { "type": "object" } },
              "modes": { "type": "array", "items": { "type": "string" } }
            }
          }
        },
        "required": ["id", "object", "created", "model", "choices", "usage", "premedice"]
      },
      "EvidenceSource": {
        "type": "object",
        "properties": {
          "title": { "type": "string", "maxLength": 200 },
          "url": { "type": "string", "maxLength": 500 },
          "source_type": { "type": "string", "maxLength": 60 },
          "id": { "type": "string", "maxLength": 120 },
          "text": { "type": "string", "maxLength": 4000 }
        }
      },
      "EvidenceVerifyRequest": {
        "type": "object",
        "properties": {
          "claim": { "type": "string", "minLength": 1, "maxLength": 2000 },
          "sources": {
            "type": "array",
            "maxItems": 30,
            "items": { "$ref": "#/components/schemas/EvidenceSource" }
          }
        },
        "required": ["claim"]
      },
      "EvidenceVerifyResponse": {
        "type": "object",
        "properties": {
          "claim": { "type": "string" },
          "verification": {
            "type": "object",
            "properties": {
              "supported": { "type": "boolean" },
              "confidence": { "type": "string", "enum": ["high", "medium", "low"] },
              "unsupported_parts": { "type": "array", "items": { "type": "string" } },
              "missing_evidence": { "type": "array", "items": { "type": "string" } },
              "notes": { "type": "string" }
            },
            "required": ["supported", "confidence", "unsupported_parts", "missing_evidence", "notes"]
          },
          "checked": { "type": "boolean" }
        },
        "required": ["claim", "verification", "checked"]
      },
      "JsonRpcRequest": {
        "type": "object",
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "number", "null"] },
          "method": {
            "type": "string",
            "enum": [
              "initialize",
              "notifications/initialized",
              "ping",
              "tools/list",
              "tools/call",
              "prompts/list",
              "prompts/get"
            ]
          },
          "params": { "type": "object" }
        },
        "required": ["jsonrpc", "method"]
      },
      "JsonRpcResponse": {
        "type": "object",
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "number", "null"] },
          "result": {},
          "error": {
            "type": "object",
            "properties": {
              "code": { "type": "integer" },
              "message": { "type": "string" }
            },
            "required": ["code", "message"]
          }
        },
        "required": ["jsonrpc", "id"]
      }
    }
  }
}
