How to Build an AI Agent with GPT-5
In this guide, you'll build a production-ready AI agent using GPT-5 via Langbase SDK. GPT-5 excels at multimodal understanding, vision capabilities, and delivering fast, accurate responses across text and images.
Let's build your first AI agent with GPT-5!
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 gpt5-agent && cd gpt5-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 GPT-4o:
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 OpenAI API key to your account to use GPT-5.
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 OpenAI API key. This allows Langbase to use GPT-4o 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 gpt5Agent = await langbase.pipes.create({
12 name: 'gpt5-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: 'openai:gpt-5',
20 });
21}Step #7Run your AI agent
Add the following code to your agent.ts file to run your agent:
1async function runGPT5Agent() {
2
3 const pipeAgents = await langbase.pipes.list();
4 const isPipeAgentExists = pipeAgents.find(pipe => pipe.name === 'gpt5-agent');
5
6 if (!isPipeAgentExists) {
7 await createGPT5Agent();
8 }
9
10 const response = await langbase.pipes.run({
11 name: 'gpt5-agent',
12 messages: [
13 {
14 role: 'user',
15 content: 'Explain how to implement a binary search algorithm in Python.'
16 }
17 ],
18 model: 'openai:gpt-5-2025-08-07'
19 });
20
21 console.log(response.completion);
22}
23
24runGPT5Agent();1npx tsx agent.tsYou should see GPT-5'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: 'gpt5-agent',
4 messages: [
5 { role: 'user', content: 'What is Langbase?' }
6 ],
7 memory: [{ name: 'support-memory-agent' }],
8 model: 'openai:gpt-5-2025-08-07'
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: 'gpt5-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: 'gpt5-agent',
44 messages: toolResults,
45 tools: [weatherToolSchema],
46 model: 'openai:gpt-5-2025-08-07'
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 GPT-5 and Langbase!