class XAIProvider(ModelProvider):
"""xAI API provider."""
def __init__(self, api_key: str | None = None):
super().__init__()
self.api_key = api_key or os.environ.get("XAI_API_KEY")
if not self.api_key:
msg = "xAI API key not found in parameters or XAI_API_KEY env var"
raise RuntimeError(msg)
self.base_url = "https://api.x.ai/v1"
self.headers = {"Authorization": f"Bearer {self.api_key}"}
self.params = {}
def is_available(self) -> bool:
"""Check whether the provider is available for use."""
return bool(self.api_key)
def _parse_model(self, data: dict[str, Any]) -> ModelInfo:
"""Parse xAI API response into ModelInfo."""
# Extract basic info
model_id = str(data["id"])
name = str(data.get("name", model_id))
owned_by = str(data.get("owned_by"))
# Parse modalities
input_modalities: set[Modality] = set()
output_modalities: set[Modality] = set()
for modality in data.get("input_modalities", []):
if modality in {"text", "image", "audio", "video"}:
input_modalities.add(modality)
for modality in data.get("output_modalities", []):
if modality in {"text", "image", "audio", "video"}:
output_modalities.add(modality)
# Set defaults if empty
if not input_modalities:
input_modalities = {"text"}
if not output_modalities:
output_modalities = {"text"}
# Token prices are USD cents per 100M tokens (12500 = $1.25/M): / 1e10 for $/token.
# The unit of search_price is unverified (0 for all models so far): kept as is.
pricing = None
if any(
key in data
for key in [
"prompt_text_token_price",
"completion_text_token_price",
"prompt_image_token_price",
"search_price",
]
):
pricing = ModelPricing(
prompt=data.get("prompt_text_token_price", 0) / _PRICE_SCALE
if "prompt_text_token_price" in data
else None,
completion=data.get("completion_text_token_price", 0) / _PRICE_SCALE
if "completion_text_token_price" in data
else None,
image=data.get("prompt_image_token_price", 0) / _PRICE_SCALE
if "prompt_image_token_price" in data
else None,
input_cache_read=data.get("cached_prompt_text_token_price", 0) / _PRICE_SCALE
if "cached_prompt_text_token_price" in data
else None,
web_search=data.get("search_price", 0) / 1_000_000
if "search_price" in data
else None,
)
# Parse created timestamp
created_at = None
if created_timestamp := data.get("created"):
with contextlib.suppress(ValueError, TypeError, OverflowError):
created_at = datetime.fromtimestamp(created_timestamp)
# Build metadata
metadata = {}
if "fingerprint" in data:
metadata["fingerprint"] = data["fingerprint"]
if "version" in data:
metadata["version"] = data["version"]
if data.get("aliases"):
metadata["aliases"] = data["aliases"]
return ModelInfo(
id=model_id,
name=name,
provider="xai",
owned_by=owned_by,
pricing=pricing,
input_modalities=input_modalities,
output_modalities=output_modalities,
created_at=created_at,
metadata=metadata,
)
async def get_models(self) -> list[ModelInfo]:
"""Override the base method to handle xAI's unique API structure."""
from anyenv import HttpError, get_json
url = f"{self.base_url}/language-models"
try:
data = await get_json(
url,
headers=self.headers,
params=self.params,
cache=True,
cache_ttl=MODELS_CACHE_TTL,
return_type=dict,
)
if "models" not in data:
msg = "Invalid response format from xAI API"
raise RuntimeError(msg)
return [self._parse_model(item) for item in data["models"]]
except HttpError as e:
msg = f"Failed to fetch models from xAI: {e}"
raise RuntimeError(msg) from e