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.
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
openaiSDK with custombase_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:
XAI_API_KEY- Preferred, checked firstOPENAI_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 modelgrok-3- Previous generation flagship modelgrok-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 togrok-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 usagebasic_anthropic.py- Basic Anthropic SDK usagebasic_gemini.py- Basic Gemini SDK usageopenrouter_migration.py- Migrating from OpenRoutermulti_provider.py- Using multiple providersstreaming.py- Streaming responsesasync_example.py- Async client usagedebug_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:
- Create a provider class inheriting from
BaseProviderinsrc/just_grok_it/providers/ - Implement:
is_installed(),patch(),unpatch() - Add format converters in
src/just_grok_it/_converters/if needed - 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/adarshdigievo/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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file just_grok_it-0.1.1.tar.gz.
File metadata
- Download URL: just_grok_it-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c437bc460831779f116f381e8949cd5ff837ff5e82efdda040de8354e27980ae
|
|
| MD5 |
8c7f0452098b60aeab5678fb279f26c5
|
|
| BLAKE2b-256 |
1b77bd963823391471a0a5319eaf9fb21c23cf10d29db30cba3b8a72e96564a1
|
Provenance
The following attestation bundles were made for just_grok_it-0.1.1.tar.gz:
Publisher:
python-publish.yml on adarshdigievo/just-grok-it
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
just_grok_it-0.1.1.tar.gz -
Subject digest:
c437bc460831779f116f381e8949cd5ff837ff5e82efdda040de8354e27980ae - Sigstore transparency entry: 1109253751
- Sigstore integration time:
-
Permalink:
adarshdigievo/just-grok-it@ca6aef653ee7c48128c8cf57040f0756c0d530d9 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/adarshdigievo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ca6aef653ee7c48128c8cf57040f0756c0d530d9 -
Trigger Event:
release
-
Statement type:
File details
Details for the file just_grok_it-0.1.1-py3-none-any.whl.
File metadata
- Download URL: just_grok_it-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a78da9e4547d93bb297b2a8ea09390857db2214dcab6646c3fe40ad0dfa8a884
|
|
| MD5 |
c2ea73b9e76fdf0474ae0b1f984c2d5f
|
|
| BLAKE2b-256 |
809b4bb874d2933499e0cb3ef3ad4ee77088e88fe779174af2de2b3e12fa8faf
|
Provenance
The following attestation bundles were made for just_grok_it-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on adarshdigievo/just-grok-it
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
just_grok_it-0.1.1-py3-none-any.whl -
Subject digest:
a78da9e4547d93bb297b2a8ea09390857db2214dcab6646c3fe40ad0dfa8a884 - Sigstore transparency entry: 1109253754
- Sigstore integration time:
-
Permalink:
adarshdigievo/just-grok-it@ca6aef653ee7c48128c8cf57040f0756c0d530d9 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/adarshdigievo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@ca6aef653ee7c48128c8cf57040f0756c0d530d9 -
Trigger Event:
release
-
Statement type: