How to Build an AI Agent with Claude 4.5 Sonnet
In this guide, you'll build a production-ready AI agent using Claude 4.5 Sonnet via Langbase SDK. Claude 4.5 Sonnet excels at coding tasks, technical problem-solving, and complex reasoning.
Let's build your first AI agent with Claude 4.5 Sonnet!
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 claude-agent && cd claude-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 Claude 3.5 Sonnet:
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 Anthropic API key to your account to use Claude 4.5 Sonnet.
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 Anthropic API key. This allows Langbase to use Claude 3.5 Sonnet 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 createClaudeAgent() {
10 // Create an AI agent with Claude 4.5 Sonnet
11 const claudeAgent = await langbase.pipes.create({
12 name: 'claude-agent',
13 messages: [
14 {
15 role: 'system',
16 content: 'You are a helpful AI assistant powered by Claude 3.5 Sonnet. You excel at coding and technical problem-solving.'
17 }
18 ],
19 model: 'anthropic:claude-sonnet-4-20250514',
20 });
21}Step #7Run your AI agent
Add the following code to your agent.ts file to run your agent:
1async function runClaudeAgent() {
2
3 const pipeAgents = await langbase.pipes.list();
4 const isPipeAgentExists = pipeAgents.find(pipe => pipe.name === 'claude-agent');
5
6 if (!isPipeAgentExists) {
7 await createClaudeAgent();
8 }
9
10 const response = await langbase.pipes.run({
11 name: 'claude-agent',
12 messages: [
13 {
14 role: 'user',
15 content: 'Explain how to implement a binary search algorithm in Python.'
16 }
17 ],
18 model: 'anthropic:claude-sonnet-4-20250514'
19 });
20
21 console.log(response.completion);
22}
23
24runClaudeAgent();1npx tsx agent.tsYou should see Claude'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: 'claude-agent',
4 messages: [
5 { role: 'user', content: 'What is Langbase?' }
6 ],
7 memory: [{ name: 'support-memory-agent' }],
8 model: 'anthropic:claude-sonnet-4-20250514'
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: 'claude-agent',
4 messages: [
5 {
6 role: 'user',
7 content: "What's the weather in SF?",
8 },
9 ],
10 tools: [weatherToolSchema],
11 model: 'anthropic:claude-sonnet-4-20250514'
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: 'claude-agent',
45 messages: toolResults,
46 tools: [weatherToolSchema],
47 model: 'anthropic:claude-sonnet-4-20250514'
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 Claude 4.5 Sonnet and Langbase!