This sample demonstrates integration with XAI (Grok) models using Genkit Java.
- XAI Plugin Setup - Configure Genkit with Grok models
- Flow Definitions - Create observable, traceable AI workflows
- Tool Usage - Define and use tools with automatic execution
- Text Generation - Generate text with latest Grok 4 models
- Streaming - Real-time response streaming
- Code Generation - Generate code with Grok 3
- Fast Tool Calling - Optimized agentic workflows with Grok 4.3
grok-4.3- Latest flagship model (2M context)
grok-4.20-0309-reasoning- Reasoning mode (2M context)grok-4.20-0309-non-reasoning- Non-reasoning mode (2M context)grok-4.20-multi-agent-0309- Multi-agent mode (2M context)
grok-build-0.1- Specialized for agentic coding (256K context)
- Java 21+
- Maven 3.6+
- XAI API key (get one at https://x.ai/)
# Set your XAI API key
export XAI_API_KEY=your-api-key-here
# Navigate to the sample directory
cd samples/xai
# Run the sample
./run.sh
# Or: mvn compile exec:java# Set your XAI API key
export XAI_API_KEY=your-api-key-here
# Navigate to the sample directory
cd samples/xai
# Run with Genkit CLI
genkit start -- ./run.shThe Dev UI will be available at http://localhost:4000
| Flow | Model | Description |
|---|---|---|
greeting |
- | Simple greeting flow |
chat |
grok-4.3 | Chat with latest Grok |
weatherAssistant |
grok-4.3 | Tool calling for weather |
streamingChat |
grok-4.3 | Streaming chat responses |
generateCode |
grok-build-0.1 | Agentic code generation |
analyze |
grok-4.20-0309-reasoning | Reasoning-based text analysis |
creativeWriting |
grok-4.3 | Creative writing with streaming |
Once the server is running on port 8080:
curl -X POST http://localhost:8080/api/flows/greeting \
-H 'Content-Type: application/json' \
-d '"World"'curl -X POST http://localhost:8080/api/flows/chat \
-H 'Content-Type: application/json' \
-d '"What makes you different from other AI models?"'curl -X POST http://localhost:8080/api/flows/weatherAssistant \
-H 'Content-Type: application/json' \
-d '"What is the weather in New York?"'curl -X POST http://localhost:8080/api/flows/generateCode \
-H 'Content-Type: application/json' \
-d '"Write a Java function to check if a string is a palindrome"'