
Deploying Kimi K2 on Amazon EC2
A practical guide to on how to run open-source Kimi K2 language model on AWS using a quantized approach, with step-by-step instructions from instance setup to API configuration.
Contributed by Dylan Souvage, Shivam Patel, Kaley Kaukinen
Kimi K2 , developed by Moonshot AI , is a recent open-source large language model with 32 billion activated parameters and 1 trillion total parameters. This Mixture-of-Experts model has shown competitive performance on various benchmarks in knowledge tasks, mathematics, coding, and tool use capabilities.
For this deployment guide, we're using the Unsloth Dynamic 1.8bit-bit quantized version (UD-TQ1_0) on an Amazon Elastic Compute Cloud (EC2) g6.16xlarge instance. We've chosen this configuration for cost efficiency and proof-of-concept purposes.
Unsloth is an open-source framework for LLM fine-tuning and inference that enables running models with reduced memory requirements and faster speeds. It provides optimized kernels and techniques to make large models more accessible on consumer hardware.
Quantization is a technique that reduces the precision of a model's weights (from 32-bit floating point to lower bit representations like 8-bit, 4-bit, or even 1.8-bit), significantly decreasing memory usage and increasing inference speed while attempting to maintain most of the model's performance. The 1.8-bit quantization we're using here substantially reduces the model size, making it possible to run on more modest hardware (with reduced performance).
For balance of size and accuracy, consider using less aggressive quantization options like UD-TQ2_K_XL (2-bit quantization), as recommended by the Unsloth team. For production environments, we recommend running benchmarks yourself, with more storage, low or no quantization and more powerful hardware configurations such as p4d.24xlarge instances.
This deployment approach provides a direct API endpoint for interacting with the model without middleware layers, allowing you to test Kimi K2's capabilities on your own infrastructure before potentially scaling to more robust configurations.
Disclaimer: This guide demonstrates a proof-of-concept deployment for testing purposes only, this is not recommended for production deployment.
Prerequisites
- AWS account with permissions to create EC2 instances
- Intermediate knowledge of AWS services and Linux commands
- Note: These instructions were written for AL2023 Deep Learning AMI v.2023.8.20250707 running CUDA 12.8.
Step 1: Launch an EC2 Instance
See this documentation for detailed steps on launching an EC2 instance. Launch an instance with the following details as a minimum:

- AMI: "Deep Learning Base OSS Nvidia Driver GPU AMI (Amazon Linux 2023)"
- Instance Type: g6.16xlarge instance type
- Storage: 500GB gp3 root volume
- Security group: Allow TCP (port 8080) from your IP (this will be for the API access)
- Create or select an existing key pair
Step 2: Connect to the Instance
Wait for the instance to initialize and connect via AWS Systems Manager Session Manager (AL2023 comes prepackaged with the SSM Agent).
Step 3: Install Dependencies
Update system packages and install required packages
sudo dnf update -ysudo dnf install pciutils cmake curl libcurl-devel gcc-c++ make python3-pip python3-devel -yStep 4: Set Up Environment Variables
Create model cache directory, set up environment variables and apply changes
mkdir -p ~/model_cacheecho 'export LLAMA_CACHE="$HOME/model_cache"' >> ~/.bashrcecho 'export PATH=/usr/local/cuda-12.8/bin:$PATH' >> ~/.bashrcecho 'export LD_LIBRARY_PATH=/usr/local/cuda-12.8/lib64:$LD_LIBRARY_PATH' >> ~/.bashrcecho 'export CUDA_HOME=/usr/local/cuda-12.8' >> ~/.bashrcecho 'export CUDA_VISIBLE_DEVICES=0' >> ~/.bashrcsource ~/.bashrcStep 5: Clone and Build llama.cpp
Clone the repository, configure and build with llama cpp. We use Unsloth’s fork of llama.cpp which has been updated to run Kimi-k2.
sudo git clone https://github.com/unslothai/llama.cpp
sudo cmake llama.cpp -B llama.cpp/build \-DBUILD_SHARED_LIBS=OFF \-DGGML_CUDA=ON \-DLLAMA_CURL=ON \-DCMAKE_CUDA_COMPILER=/usr/local/cuda-12.8/bin/nvcc
#Build llama-serversudo cmake --build llama.cpp/build --config Release -j --clean-first --target llama-server
#Create bin directory and copy binariesmkdir -p ~/bin cp llama.cpp/build/bin/llama-server ~/bin/Step 6: Create Server Script
Create a server script that configures the environment variables for CUDA support and launches the llama-server with recommended parameters for Kimi K2 and API access through port 8080.
cat > ~/start_kimi_server.sh << 'EOF'#!/bin/bashexport LLAMA_CACHE="$HOME/model_cache"export PATH=/usr/local/cuda-12.8/bin:$PATHexport LD_LIBRARY_PATH=/usr/local/cuda-12.8/lib64:$LD_LIBRARY_PATHexport CUDA_HOME=/usr/local/cuda-12.8export CUDA_VISIBLE_DEVICES=0~/bin/llama-server \-hf unsloth/Kimi-K2-Instruct-GGUF:UD-TQ1_0 \--cache-type-k q4_0 \--threads -1 \--n-gpu-layers 99 \--temp 0.6 \--min_p 0.01 \--ctx-size 16384 \--seed 3407 \-ot ".ffn\_.\*_exps.=CPU" \--jinja \--host 0.0.0.0 \--port 8080 \--no-webuiEOFchmod +x ~/start_kimi_server.sh
Step 7: Create Systemd Service
Create a systemd service configuration that so the Kimi K2 server runs at system startup, restarts if it fails.
sudo tee /etc/systemd/system/kimi-server.service > /dev/null << EOF[Unit]Description=Kimi K2 LLM ServerAfter=network.target[Service]Type=simpleUser=rootWorkingDirectory=/rootExecStart=/root/start_kimi_server.shRestart=on-failureRestartSec=5s[Install]WantedBy=multi-user.targetEOFStep 8: Enable and Start the Service
#Reload systemd to recognize the new servicesudo systemctl daemon-reload#Enable the service to start on bootsudo systemctl enable kimi-server#Start the servicesudo systemctl start kimi-serverNote: The model may take several hours to download, build and run. You can check the status by running:
sudo systemctl status kimi-serverExample Usage
Note: We're modifying the output using jq to extract only the content field from the JSON response, then applying sed to convert escaped newlines to actual line breaks, and finally wrapping the text in ANSI color codes with awk to highlight the content in cyan for better readability.


Usage Instructions
API Format
Send POST requests to the API endpoint with the following format, keep in mind that in Step 1 we restricted the instance to your IP, so before you launch a terminal and send the curl, ensure your IP is whitelisted.
curl -X POST http://your-instance-ip:8080/completion \-H "Content-Type: application/json" \-d '{"prompt": "<|im_system|>system<|im_middle|><SYSTEM PROMPT HERE><|im_end|><|im_user|>user<|im_middle|>YOUR PROMPT HERE<|im_end|><|im_assistant|>assistant<|im_middle|>","temperature": 0.6,"min_p": 0.01,"n_predict": 500}'Parameters
- prompt: The prompt to send to the model, following the Kimi K2 chat template format
- temperature: Controls randomness (0.6 is recommended)
- min_p: Minimum probability threshold (0.01 is recommended)
- n_predict: Maximum number of tokens to generate
Chat Template Format
<|im_system|>system<|im_middle|>SYSTEM_PROMPT<|im_end|><|im_user|>user<|im_middle|>USER_MESSAGE<|im_end|><|im_assistant|>assistant<|im_middle|>Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article