Skip to main content

Auto-redirect LLM API requests (OpenAI, Anthropic, Gemini, Mistral, Cohere, OpenRouter) to xAI APIs - Just Grok It!

Project description

Just Grok It 🚀

Auto-redirect LLM API requests to xAI APIs with a single line of code.

PyPI version Python 3.10+ License: MIT

Overview

just-grok-it is a drop-in solution that redirects LLM API calls from multiple providers to xAI's OpenAI-compatible API. This allows you to seamlessly switch your existing LLM-powered applications to use Grok models without changing any of your existing code.

Supported SDKs

Provider Package Redirect Method
OpenAI openai Direct base URL redirect
OpenRouter openrouter Official SDK patching + format conversion
Together AI openai (custom base_url) Base URL intercept + redirect
Groq openai (custom base_url) Base URL intercept + redirect
Perplexity openai (custom base_url) Base URL intercept + redirect
Anthropic anthropic Format conversion + redirect
Google Gemini google-generativeai Format conversion + redirect
Mistral AI mistralai Format conversion + redirect
Cohere cohere Format conversion + redirect

Note: OpenRouter, Together AI, Groq, and Perplexity also work when using the openai SDK with custom base_url - those requests are intercepted and redirected as well.

Installation

pip install just-grok-it

Or with uv:

uv add just-grok-it

Quick Start

Add a single line at the entry point of your application:

import just_grok_it
just_grok_it.all()  # Uses default model: grok-4-1-fast-non-reasoning

# Your existing code works as-is - all calls go to xAI!

OpenAI Example

import just_grok_it
just_grok_it.all()

from openai import OpenAI

client = OpenAI()  # Connects to xAI APIs
response = client.chat.completions.create(
    model="grok-4",
    messages=[{"role": "user", "content": "Hello, Grok!"}]
)
print(response.choices[0].message.content)

OpenRouter Example (Official SDK)

import just_grok_it
just_grok_it.all()

from openrouter import OpenRouter

# Your existing OpenRouter code - automatically redirected to xAI!
client = OpenRouter(api_key="your-key")
response = client.chat.send(
    model="anthropic/claude-3-opus",  # Will use Grok instead
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

Together / Groq Migration (via OpenAI SDK)

import just_grok_it
just_grok_it.all()

from openai import OpenAI

# Your existing Together/Groq code using OpenAI SDK - automatically redirected!
client = OpenAI(
    base_url="https://api.together.xyz/v1",  # Will be redirected!
    api_key="your-key"
)
# All calls now go to xAI

Anthropic Example

import just_grok_it
just_grok_it.all()

from anthropic import Anthropic

client = Anthropic()  # Calls get converted and sent to xAI
response = client.messages.create(
    model="claude-3-sonnet",  # Will use Grok model instead
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.content[0].text)

Google Gemini Example

import just_grok_it
just_grok_it.all()

import google.generativeai as genai

model = genai.GenerativeModel('gemini-pro')  # Uses xAI instead
response = model.generate_content("Hello!")
print(response.text)

Mistral AI Example

import just_grok_it
just_grok_it.all()

from mistralai import Mistral

client = Mistral(api_key="your-key")
response = client.chat.complete(
    model="mistral-large",  # Will use Grok instead
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

Cohere Example

import just_grok_it
just_grok_it.all()

import cohere

client = cohere.Client(api_key="your-key")
response = client.chat(
    message="Hello!",
    model="command"  # Will use Grok instead
)
print(response.text)

Configuration

API Key

Set your xAI API key as an environment variable:

export XAI_API_KEY="your-xai-api-key"

The library checks for API keys in this order:

  1. XAI_API_KEY - Preferred, checked first
  2. OPENAI_API_KEY - Fallback if XAI_API_KEY is not set

When using the OpenAI SDK without passing an api_key to the client constructor, just-grok-it will automatically use XAI_API_KEY from the environment:

import just_grok_it
just_grok_it.all()

from openai import OpenAI
client = OpenAI()  # Uses XAI_API_KEY from environment automatically

Get your API key from the xAI Console.

Default Model

The default model is grok-4-1-fast-non-reasoning. You can change it:

import just_grok_it
just_grok_it.all(default_model='grok-4')  # Use grok-4 instead

Available Models

  • grok-4-1-fast-non-reasoning - Fast, efficient model (default)
  • grok-4 - Latest and most capable model
  • grok-3 - Previous generation flagship model
  • grok-3-mini - Faster, lightweight model

See xAI documentation for the complete list.

API Reference

just_grok_it.all(default_model=None)

Main entry point. Activates xAI API redirection for all installed SDKs.

Parameters:

  • default_model (str, optional): Default model. Defaults to grok-4-1-fast-non-reasoning.

Returns:

  • dict: Provider names mapped to patch success status.
results = just_grok_it.all()
# {'openai': True, 'openrouter': True, 'anthropic': True, 'gemini': False, 'mistral': False, 'cohere': False}

just_grok_it.unpatch_all()

Removes all patches and restores original SDK behavior.

just_grok_it.is_patched(provider_name=None)

Check if SDKs are patched.

just_grok_it.get_installed_providers()

Returns list of installed provider names.

just_grok_it.get_patched_providers()

Returns list of currently patched provider names.

just_grok_it.DEFAULT_XAI_MODEL

The default model constant: grok-4-1-fast-non-reasoning

just_grok_it.XAI_BASE_URL

The xAI API base URL: https://api.x.ai/v1

Debug Logging

Enable debug logging to see interception messages:

import logging
logging.basicConfig(level=logging.DEBUG)

import just_grok_it
just_grok_it.all()

Output:

DEBUG:just_grok_it:[just_grok_it] Initialized with default model: grok-4-1-fast-non-reasoning
DEBUG:just_grok_it:[just_grok_it] Patched providers: openai, openrouter, anthropic, gemini, mistral, cohere
DEBUG:just_grok_it:[just_grok_it] All requests will now be redirected to https://api.x.ai/v1

When making API calls:

DEBUG:just_grok_it:[just_grok_it] openai: Intercepted OpenAI initialization, redirecting to xAI API: https://api.x.ai/v1
DEBUG:just_grok_it:[just_grok_it] anthropic: Intercepted messages.create request, redirecting to xAI API with model: grok-4

Examples

See the examples/ directory for complete usage examples:

  • basic_openai.py - Basic OpenAI SDK usage
  • basic_anthropic.py - Basic Anthropic SDK usage
  • basic_gemini.py - Basic Gemini SDK usage
  • openrouter_migration.py - Migrating from OpenRouter
  • multi_provider.py - Using multiple providers
  • streaming.py - Streaming responses
  • async_example.py - Async client usage
  • debug_logging.py - Debug logging

How It Works

OpenAI SDK (and compatible providers via base_url)

The library monkey-patches the OpenAI client __init__ to override the base_url to https://api.x.ai/v1, regardless of the original URL. This covers OpenAI direct usage, and any provider that uses the OpenAI SDK with a custom base_url (Together, Groq, Perplexity, etc.).

Official OpenRouter SDK

The library patches the openrouter.chat.Chat.send() method to intercept calls, convert to OpenAI format, send to xAI, and convert the response back to OpenRouter format.

Native SDKs (Anthropic, Gemini, Mistral, Cohere)

The library intercepts API calls, converts the request format to OpenAI format, sends to xAI, and converts the response back to the original format.

Compatibility

  • Python: 3.10, 3.11, 3.12, 3.13, 3.14
  • OpenAI SDK: 1.0.0+
  • OpenRouter SDK: 0.1.0+
  • Anthropic SDK: 0.18.0+
  • Google GenAI SDK: 0.5.0+
  • Mistral AI SDK: 0.1.0+
  • Cohere SDK: 5.0.0+

Adding New Providers

The library is designed to be extensible:

  1. Create a provider class inheriting from BaseProvider in src/just_grok_it/providers/
  2. Implement: is_installed(), patch(), unpatch()
  3. Add format converters in src/just_grok_it/_converters/ if needed
  4. Register in src/just_grok_it/providers/__init__.py

Development

Setting Up Development Environment

This project uses uv for dependency management.

# Clone the repository
git clone https://github.com/just-grok-it/just-grok-it.git
cd just-grok-it

# Install all development dependencies (recommended)
# This includes: pytest, all provider SDKs, and integration test deps
uv sync

# Run tests
uv run pytest

Running Tests

# Run all tests
uv run pytest

# Run with verbose output
uv run pytest -v

# Run tests for a specific provider
uv run pytest tests/openai/
uv run pytest tests/anthropic/
uv run pytest tests/gemini/
uv run pytest tests/mistral/
uv run pytest tests/cohere/
uv run pytest tests/openrouter/

# Run with debug logging
uv run pytest -v --log-cli-level=DEBUG

Installing for End Users

For end users (not development), install with pip:

# Basic install (just OpenAI SDK included)
pip install just-grok-it

# Install with specific provider SDKs
pip install just-grok-it[anthropic]
pip install just-grok-it[openrouter]
pip install just-grok-it[gemini]
pip install just-grok-it[mistral]
pip install just-grok-it[cohere]

# Install with all provider SDKs
pip install just-grok-it[all-providers]

License

MIT License - see LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

just_grok_it-0.1.0.tar.gz (110.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

just_grok_it-0.1.0-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file just_grok_it-0.1.0.tar.gz.

File metadata

  • Download URL: just_grok_it-0.1.0.tar.gz
  • Upload date:
  • Size: 110.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for just_grok_it-0.1.0.tar.gz
Algorithm Hash digest
SHA256 16ad9d3e3e065ab14703060b9bac5e23f6ed75c5b611e6175d3e68535a37c814
MD5 4b9308d51635b2a81c08a439045d9d12
BLAKE2b-256 ec04b121237eed0bc9bda9eac160c66bf09413547b255ebb7297c29a4f6aeeec

See more details on using hashes here.

Provenance

The following attestation bundles were made for just_grok_it-0.1.0.tar.gz:

Publisher: python-publish.yml on adarshdigievo/just-grok-it

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file just_grok_it-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: just_grok_it-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for just_grok_it-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed3b8fad018d740b477f46647ce8b54554ec9621244adf58f6b83025d66bba10
MD5 ff642938101e73608e098c376f3cfd45
BLAKE2b-256 1d989f93a03f6e939a3af74fce429bfd6d19902687ca3b57bc4a02f460f2a7ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for just_grok_it-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on adarshdigievo/just-grok-it

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page