Contact Support

    Guides

    More guides
    How to Build an AI Agent with Kimi K2

    How to Build an AI Agent with Kimi K2

    Nov 05 20255 min read

    In this guide, you'll build a production-ready AI agent using Kimi K2 via Langbase SDK. Kimi K2 is a powerful AI model that excels at multilingual capabilities, coding tasks, and enterprise-grade performance.

    Let's build your first AI agent with Kimi K2!

    Step #0Prerequisites

    Before you begin, create a free account on Langbase.

    Step #1Setup your project

    Create a new directory for your AI agent project:

    1mkdir kimi-k2-agent && cd kimi-k2-agent

    Step #2Initialize the project

    Initialize a Node.js project and create a TypeScript file:

    1npm init -y && touch agent.ts

    Step #3Install Langbase SDK

    Install the Langbase SDK to interact with Kimi K2:

    1npm install langbase dotenv

    Step #4Get your Langbase API key

    Every request to Langbase requires an API key. Generate your API key by following these steps. Add Together AI API key to your account to use Kimi K2. Create a .env file in your project root:

    1# Replace with your actual Langbase API key 2LANGBASE_API_KEY=your_api_key_here

    Step #5Add LLM API keys

    Navigate to LLM API keys page and add your Mistral AI API key. This allows Langbase to use Mistral Large 2 on your behalf.

    Step #6Create your first AI agent

    Add the following code to your agent.ts file:

    1import 'dotenv/config'; 2import { Langbase } from 'langbase'; 3 4// Initialize Langbase SDK 5const langbase = new Langbase({ 6 apiKey: process.env.LANGBASE_API_KEY! 7}); 8 9async function createKimiK2Agent() { 10 // Create an AI agent with Kimi K2 11 const kimiK2Agent = await langbase.pipes.create({ 12 name: 'kimi-k2-agent', 13 messages: [ 14 { 15 role: 'system', 16 content: 'You are a helpful AI assistant powered by Kimi K2. You excel at coding and technical problem-solving.' 17 } 18 ], 19 model: 'together:moonshotai/Kimi-K2-Instruct', 20 }); 21}

    Step #7Run your AI agent

    Add the following code to your agent.ts file to run your agent:

    1async function runKimiK2Agent() { 2 3 const pipeAgents = await langbase.pipes.list(); 4 const isPipeAgentExists = pipeAgents.find(pipe => pipe.name === 'kimi-k2-agent'); 5 6 if (!isPipeAgentExists) { 7 await createKimiK2Agent(); 8 } 9 10 const response = await langbase.pipes.run({ 11 name: 'kimi-k2-agent', 12 messages: [ 13 { 14 role: 'user', 15 content: 'Explain how to implement a binary search algorithm in Python.' 16 } 17 ], 18 model: 'together:moonshotai/Kimi-K2-Instruct' 19 }); 20 21 console.log(response.completion); 22} 23 24runKimiK2Agent();
    1npx tsx agent.ts

    You should see Kimi K2's response explaining the binary search algorithm!

    Step #8Add Memory (RAG)

    Give your agent access to documents for context-aware responses:

    1 2// Upload a document to memory 3const memory = await langbase.memories.create({ 4 name: 'support-memory-agent', 5 description: 'Support chatbot memory agent', 6}); 7 8const content = 'Langbase is a platform for building AI agents. It provides a set of tools and APIs to build AI agents.'; 9const documentBlob = new Blob([content], { type: 'text/plain' }); 10const document = new File([documentBlob], 'langbase-faqs.txt', { type: 'text/plain' }); 11 12const response = await langbase.memories.documents.upload({ 13 document, 14 memoryName: 'support-memory-agent', 15 contentType: 'text/plain', 16 documentName: 'langbase-faqs.txt', 17});

    Run the Pipe Agent with memory just add the memory name to the parameters:

    1// Query with memory 2const response = await langbase.pipes.run({ 3 name: 'kimi-k2-agent', 4 messages: [ 5 { role: 'user', content: 'What is Langbase?' } 6 ], 7 memory: [{ name: 'support-memory-agent' }], 8 model: 'together:moonshotai/Kimi-K2-Instruct' 9}); 10 11console.log(response.completion);

    Step #9Add Tool Calling

    Enable your agent to call external functions:

    1const response = await langbase.pipes.run({ 2 stream: false, 3 name: 'kimi-k2-agent', 4 messages: [ 5 { 6 role: 'user', 7 content: "What's the weather in SF?", 8 }, 9 ], 10 tools: [weatherToolSchema], 11}); 12 13const toolCalls = await getToolsFromRun(response); 14const hasToolCalls = toolCalls.length > 0; 15const threadId = response.threadId; 16 17if (hasToolCalls) { 18 // Process each tool call 19 const toolResultPromises = toolCalls.map(async (toolCall): Promise<Message> => { 20 const toolName = toolCall.function.name; 21 const toolParameters = JSON.parse(toolCall.function.arguments); 22 const toolFunction = tools[toolName as keyof typeof tools]; 23 24 // Call the tool function with the parameters 25 const toolResponse = await toolFunction(toolParameters); 26 27 // Return the tool result 28 return { 29 role: 'tool', 30 name: toolName, 31 content: toolResponse, 32 tool_call_id: toolCall.id, 33 }; 34 }); 35 36 // Wait for all tool calls to complete 37 const toolResults = await Promise.all(toolResultPromises); 38 39 // Call the agent pipe again with the updated messages 40 const finalResponse = await langbase.pipes.run({ 41 threadId, 42 stream: false, 43 name: 'kimi-k2-agent', 44 messages: toolResults, 45 tools: [weatherToolSchema], 46 model: 'together:moonshotai/Kimi-K2-Instruct' 47 }); 48 49 console.log(JSON.stringify(finalResponse, null, 2)); 50} else { 51 console.log('Direct response (no tools called):'); 52 console.log(JSON.stringify(response, null, 2)); 53} 54 55 56// Mock implementation of the weather function 57async function getCurrentWeather(args: { location: string }) { 58 return 'Sunny, 75°F'; 59} 60 61// Weather tool schema 62const weatherToolSchema: Tools = { 63 type: 'function', 64 function: { 65 name: 'getCurrentWeather', 66 description: 'Get the current weather of a given location', 67 parameters: { 68 type: 'object', 69 required: ['location'], 70 properties: { 71 unit: { 72 enum: ['celsius', 'fahrenheit'], 73 type: 'string', 74 }, 75 location: { 76 type: 'string', 77 description: 'The city and state, e.g. San Francisco, CA', 78 }, 79 }, 80 }, 81 }, 82}; 83 84// Object to hold all tools 85const tools = { 86 getCurrentWeather 87};

    Step #10Deploy to production

    Your AI agent is production-ready from the start with Langbase:

    • Serverless Infrastructure - Scales automatically from 1 to 1M requests
    • Multi-Model Support - Switch between 600+ models without code changes
    • Real-time Analytics - Track performance, usage, and costs
    • Built-in Tracing - Debug and monitor every request

    Next steps

    Explore more advanced features:

    Build powerful AI agents with Kimi K2 and Langbase!