A production-ready, interactive command-line chatbot powered by xAI's Grok models with autonomous research capabilities, image analysis, and persistent conversation history.
- Web Search: Real-time internet search for current information
- X (Twitter) Search: Access trending topics and social media discussions
- Code Execution: Run Python code for calculations and data analysis
- Analyze images with detailed descriptions
- Automatic image preprocessing to stay under gRPC message size limits
- Support for JPG/JPEG and PNG formats
- JSONL-based storage (one message per line)
- Resume previous conversations
- No Assumed State principle - deterministic rebuilds from ground truth
grok-4- Higher quality, slowergrok-4-fast- Recommended for agentic workflowsgrok-4-fast-non-reasoning- Fastest, no reasoning tokens
- Live tool call visibility
- Token usage tracking (prompt, completion, reasoning, image tokens)
- Citations display
###1. Clone the repository
git clone https://github.com/yourusername/xai-grok-python-cli-bot
cd xai-grok-python-cli-botpip install -r requirements.txtCopy the example environment file and add your xAI API key:
cp .env.example .envEdit .env and add your API key:
XAI_API_KEY=your_xai_api_key_here
Get your API key from: https://console.x.ai/
python grok_chat.py# Start with default settings
python grok_chat.py
# Use a specific model
python grok_chat.py --model grok-4
# Resume a previous conversation
python grok_chat.py --resume ca6ae2b8
# Use a custom persona
python grok_chat.py --persona personas/custom_persona.md
# Combine options
python grok_chat.py -m grok-4 -r ca6ae2b8While chatting, you can use these commands:
/model- Show current model/model grok-4- Switch to a different model/list- Show all saved conversations/resume <id>- Resume a specific conversation/new- Start a new conversationimage:<path>- Analyze an imageexit,quit,bye- End session
You: What are the latest developments in AI?
[π§ Calling: web_search]
Assistant: Based on recent search results, here are the latest AI developments...
You: image:/path/to/image.jpg
β Image loaded: image.jpg (2345.6 KB)
What should I do with this image? Analyze this image in detail
Assistant: This image shows...
You: /list
Found 3 conversation(s):
4a3d8c91 - 2025-11-08 12:34:56 - 8 messages
ca6ae2b8 - 2025-11-09 02:39:35 - 12 messages (current)
e7f2a1b9 - 2025-11-09 03:15:22 - 5 messages
This bot follows the "No Ass" (No Assumed State) principle:
- Ground Truth: JSONL file is the single source of truth
- Fresh API Calls: Create a new chat object on every turn
- Deterministic Rebuilds: Conversation history rebuilt from disk each time
- No Persistent SDK State: Prevents tool result pollution and state accumulation bugs
Each message is stored as a single JSON line:
{
"id": "uuid",
"role": "user|assistant",
"timestamp": "ISO8601",
"content": "message text",
"input_tokens": 0,
"output_tokens": 0,
"image": {"image_url": "data:...", "detail": "high"} // optional
}conversations/
βββ [8-char-uuid]/
βββ messages.jsonl # Ground truth conversation log
βββ metadata.json # Conversation metadata
Images are automatically preprocessed to stay under xAI's gRPC 4 MB message size limit:
- Target: 2 MB max encoded size (~1.5 MB binary)
- Automatic resizing with high-quality LANCZOS resampling
- Iterative compression (quality 95 β 85 β 75 β 65)
- RGBA β RGB conversion with white background
- Only the most recent image is included in message history to prevent exponential growth
Create a markdown file in personas/ directory:
# My Custom Persona
You are a [description]...
## Capabilities
- [capability 1]
- [capability 2]
## Response Style
- [guideline 1]
- [guideline 2]Use it with:
python grok_chat.py --persona personas/my_custom_persona.mdEdit the GrokChat class initialization in grok_chat.py:
self.max_history = 50 # Number of messages to keep in context windowIf you get "message larger than max" errors:
- Images are automatically resized, but extremely large files may still fail
- Try using a smaller source image
- Check that image format is JPG/JPEG or PNG
Error: XAI_API_KEY not found in .env file
Make sure:
.envfile exists in the project root- File contains
XAI_API_KEY=your_key_here - No quotes around the key value
Error: xAI SDK not found or outdated
Install requirements:
pip install -r requirements.txt- Context Window Management: No automatic overflow handling beyond
max_historylimit - Image History: Previous images are not re-sent in conversation history (only text context retained)
- Model Persistence: Model changes don't persist across script restarts
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT License - see LICENSE file for details
- Built with xAI SDK
- Image processing with Pillow
- Architecture inspired by the "No Assumed State" principle
- GitHub Issues: https://github.com/yourusername/xai-grok-python-cli-bot/issues
- xAI Documentation: https://docs.x.ai/
- xAI Console: https://console.x.ai/
Note: This is an unofficial community project. For official xAI support, visit https://x.ai/