How to Build an AI Agent with Gemini 2.5 Pro
In this guide, you'll build a production-ready AI agent using Gemini 2.5 Pro via Langbase SDK. Gemini 2.5 Pro excels at handling long context windows, multimodal understanding, and advanced reasoning tasks.
Let's build your first AI agent with Gemini 2.5 Pro!
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 gemini-agent && cd gemini-agentStep #2Initialize the project
Initialize a Node.js project and create a TypeScript file:
1npm init -y && touch agent.tsStep #3Install Langbase SDK
Install the Langbase SDK to interact with Gemini 2.5 Pro:
1npm install langbase dotenvStep #4Get your Langbase API key
Every request to Langbase requires an API key. Generate your API key by following these steps.
Add Google AI API key to your account to use Gemini 2.5 Pro.
Create a .env file in your project root:
1# Replace with your actual Langbase API key
2LANGBASE_API_KEY=your_api_key_hereStep #5Add LLM API keys
Navigate to LLM API keys page and add your Google AI API key. This allows Langbase to use Gemini 2.5 Pro 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 createGeminiAgent() {
10 // Create an AI agent with Gemini 2.5 Pro
11 const geminiAgent = await langbase.pipes.create({
12 name: 'gemini-agent',
13 messages: [
14 {
15 role: 'system',
16 content: 'You are a helpful AI assistant powered by Gemini 2.5 Pro. You excel at coding and technical problem-solving.'
17 }
18 ],
19 model: 'google:gemini-2.5-pro',
20 });
21}Step #7Run your AI agent
Add the following code to your agent.ts file to run your agent:
1async function runGeminiAgent() {
2
3 const pipeAgents = await langbase.pipes.list();
4 const isPipeAgentExists = pipeAgents.find(pipe => pipe.name === 'gemini-agent');
5
6 if (!isPipeAgentExists) {
7 await createGeminiAgent();
8 }
9
10 const response = await langbase.pipes.run({
11 name: 'gemini-agent',
12 messages: [
13 {
14 role: 'user',
15 content: 'Explain how to implement a binary search algorithm in Python.'
16 }
17 ],
18 model: 'google:gemini-2.5-pro'
19 });
20
21 console.log(response.completion);
22}
23
24runGeminiAgent();1npx tsx agent.tsYou should see Gemini'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: 'gemini-agent',
4 messages: [
5 { role: 'user', content: 'What is Langbase?' }
6 ],
7 memory: [{ name: 'support-memory-agent' }],
8 model: 'google:gemini-2.5-pro'
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: 'gemini-agent',
4 messages: [
5 {
6 role: 'user',
7 content: "What's the weather in SF?",
8 },
9 ],
10 tools: [weatherToolSchema],
11 model: 'google:gemini-2.5-pro'
12});
13
14const toolCalls = await getToolsFromRun(response);
15const hasToolCalls = toolCalls.length > 0;
16const threadId = response.threadId;
17
18if (hasToolCalls) {
19 // Process each tool call
20 const toolResultPromises = toolCalls.map(async (toolCall): Promise<Message> => {
21 const toolName = toolCall.function.name;
22 const toolParameters = JSON.parse(toolCall.function.arguments);
23 const toolFunction = tools[toolName as keyof typeof tools];
24
25 // Call the tool function with the parameters
26 const toolResponse = await toolFunction(toolParameters);
27
28 // Return the tool result
29 return {
30 role: 'tool',
31 name: toolName,
32 content: toolResponse,
33 tool_call_id: toolCall.id,
34 };
35 });
36
37 // Wait for all tool calls to complete
38 const toolResults = await Promise.all(toolResultPromises);
39
40 // Call the agent pipe again with the updated messages
41 const finalResponse = await langbase.pipes.run({
42 threadId,
43 stream: false,
44 name: 'gemini-agent',
45 messages: toolResults,
46 tools: [weatherToolSchema],
47 model: 'google:gemini-2.5-pro'
48 });
49
50 console.log(JSON.stringify(finalResponse, null, 2));
51} else {
52 console.log('Direct response (no tools called):');
53 console.log(JSON.stringify(response, null, 2));
54}
55
56
57// Mock implementation of the weather function
58async function getCurrentWeather(args: { location: string }) {
59 return 'Sunny, 75°F';
60}
61
62// Weather tool schema
63const weatherToolSchema: Tools = {
64 type: 'function',
65 function: {
66 name: 'getCurrentWeather',
67 description: 'Get the current weather of a given location',
68 parameters: {
69 type: 'object',
70 required: ['location'],
71 properties: {
72 unit: {
73 enum: ['celsius', 'fahrenheit'],
74 type: 'string',
75 },
76 location: {
77 type: 'string',
78 description: 'The city and state, e.g. San Francisco, CA',
79 },
80 },
81 },
82 },
83};
84
85// Object to hold all tools
86const tools = {
87 getCurrentWeather
88};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 Gemini 2.5 Pro and Langbase!