X

langbase com

Bing Rank
Average Position of Bing Search Engine Ranking of related query such as 'Sales AI Agent', 'Coding AI Agent', etc.

Last Updated: 2025-04-15

Information

→ Dismiss ⌘ NEW Agent Architectures Composable AI Email Agents ⌘ Step #0 Step #0 # mkdir ai-email-agent && cd ai-email-agent mkdir ai-email-agent && cd ai-email-agent Copy Copied! npm init -y && touch index.ts && touch agents.ts npm init -y && touch index.ts && touch agents.ts Copy Copied! npm i langbase dotenv npm i langbase dotenv Copy Copied! Step #1 Step #1 # LANGBASE_API_KEY=xxxxxxxxx LANGBASE_API_KEY = xxxxxxxxx Copy Copied! Step #3 Step #3 # Step #4 Step #4 # Step #4 Step #4 # import { Langbase } from 'langbase'; import { Langbase } from ' langbase ' ; import 'dotenv/config' import ' dotenv/config ' const langbase = new Langbase({ apiKey: process.env.LANGBASE_API_KEY! }); const langbase = new Langbase ({ apiKey : process . env . LANGBASE_API_KEY ! }); // Sentiment analysis // Sentiment analysis export const emailSentimentAgent = async (emailContent: string) => { export const emailSentimentAgent = async ( emailContent : string ) => { const response = await langbase.pipes.run({ const response = await langbase . pipes . run ({ stream: false, stream : false , json: true, json : true , name: 'email-sentiment', name : ' email-sentiment ' , messages: [ messages : [ { { role: 'system', role : ' system ' , content: `You are a sentiment analyzer. content : `You are a sentiment analyzer. You will analyze user email sentiment. You will analyze user email sentiment. Respond in JSON with "sentiment" key Respond in JSON with "sentiment" key Pick from happy | frustrated | neutral | sad` Pick from happy | frustrated | neutral | sad` }, }, { { role: 'user', role : ' user ' , content: emailContent content : emailContent } } ] ] }); }); const completion = JSON.parse(response.completion); const completion = JSON . parse ( response . completion ); return completion.sentiment; return completion . sentiment ; }; }; Copy Copied! AI user experience Step #5 Step #5 # // Summarize email // Summarize email export const emailSummaryAgent = async (emailContent: string) => { export const emailSummaryAgent = async ( emailContent : string ) => { const response = await langbase.pipes.run({ const response = await langbase . pipes . run ({ json: true, json : true , stream: false, stream : false , name: 'summarizer', name : ' summarizer ' , messages: [ messages : [ { { role: 'system', role : ' system ' , content: `You are a content summarizer. You will summarize content content : `You are a content summarizer. You will summarize content without loosing context into less wordy to the point version.` without loosing context into less wordy to the point version.` }, }, { { role: 'user', role : ' user ' , content: emailContent content : emailContent } } ] ] }); }); const completion = JSON.parse(response.completion); const completion = JSON . parse ( response . completion ); return completion.summary; return completion . summary ; }; }; Copy Copied! Step #6 Step #6 # // Determine if a response is needed // Determine if a response is needed export const shouldRespondToEmailAgent = async (summary: string, sentiment: string) => { export const shouldRespondToEmailAgent = async ( summary : string , sentiment : string ) => { const response = await langbase.pipes.run({ const response = await langbase . pipes . run ({ json: true, json : true , stream: false, stream : false , name: 'decision-maker', name : ' decision-maker ' , messages: [ messages : [ { { role: 'system', role : ' system ' , content: `You are a decision maker that analyzes and decides if the given email requires a response or not. content : `You are a decision maker that analyzes and decides if the given email requires a response or not. Make sure to check if the email is spam or not. If the email is spam, then it does not need a response. Make sure to check if the email is spam or not. If the email is spam, then it does not need a response. If it requires a response, based on the email urgency, decide the response date. Also define the response priority. If it requires a response, based on the email urgency, decide the response date. Also define the response priority. Use following keys and values accordingly Use following keys and values accordingly - respond: true | false - respond: true | false - category: primary | spam - category: primary | spam - priority: urgent | high | medium | low` - priority: urgent | high | medium | low` }, }, { { role: 'user', role : ' user ' , content: `Email Summary: ${summary} content : `Email Summary: ${ summary } Email sentiment: ${sentiment}` Email sentiment: ${ sentiment } ` } } ], ], }); }); const completion = JSON.parse(response.completion); const completion = JSON . parse ( response . completion ); return completion.respond; return completion . respond ; }; }; Copy Copied! Step #7 Step #7 # // Pick an email writer // Pick an email writer export const pickEmailWriterAgent = async (summary: string, sentiment: string) => { export const pickEmailWriterAgent = async ( summary : string , sentiment : string ) => { const response = await langbase.pipes.run({ const response = await langbase . pipes . run ({ json: true, json : true , stream: false, stream : false , name: 'pick-email-writer', name : ' pick-email-writer ' , messages: [ messages : [ { { role: 'system', role : ' system ' , content: content : `You are an email tone picker that analyzes the input `You are an email tone picker that analyzes the input and picks up the response email tone. and picks up the response email tone. Pick from: professional | formal | informal | casual | friendly` Pick from: professional | formal | informal | casual | friendly` }, }, { { role: 'user', role : ' user ' , content: `Email Summary: ${summary} content : `Email Summary: ${ summary } Email sentiment: ${sentiment}` Email sentiment: ${ sentiment } ` } } ], ], }); }); const completion = JSON.parse(response.completion); const completion = JSON . parse ( response . completion ); return completion.tone; return completion . tone ; }; }; Copy Copied! Step #8 Step #8 # // Generate an email reply // Generate an email reply export const emailResponseAgent = async (writer: string, summary: string) => { export const emailResponseAgent = async ( writer : string , summary : string ) => { const { stream } = await langbase.pipes.run({ const { stream } = await langbase . pipes . run ({ stream: true, stream : true , name: 'email-writer', name : ' email-writer ' , messages: [ messages : [ { { role: 'system', role : ' system ' , content: `You are an email writer that writes a concise content : `You are an email writer that writes a concise to the point well written email as a reply to a user email.`, to the point well written email as a reply to a user email.` , }, }, { { role: 'user', role : ' user ' , content: `Write a response using the following information: content : `Write a response using the following information: Email tone: ${writer} Email tone: ${ writer } User email: ${summary}`, User email: ${ summary } ` , }, }, ], ], }); }); return stream; return stream ; }; }; Copy Copied! Step #9 Step #9 # import { getRunner } from 'langbase'; import { getRunner } from ' langbase ' ; import { import { emailResponseAgent, emailResponseAgent , emailSentimentAgent, emailSentimentAgent , emailSummaryAgent, emailSummaryAgent , pickEmailWriterAgent, pickEmailWriterAgent , shouldRespondToEmailAgent, shouldRespondToEmailAgent , } from './agents'; } from ' ./agents ' ; import { stdout } from 'process'; import { stdout } from ' process ' ; const workflow = async (emailContent: string) => { const workflow = async ( emailContent : string ) => { console.log('Email:', emailContent); console . log ( ' Email: ' , emailContent ); // parallelly run the agent pipes // parallelly run the agent pipes const [emailSentiment, emailSummary] = await Promise.all([ const [ emailSentiment , emailSummary ] = await Promise . all ([ emailSentimentAgent(emailContent), emailSentimentAgent ( emailContent ), emailSummaryAgent(emailContent), emailSummaryAgent ( emailContent ), ]); ]); console.log('Sentiment:', emailSentiment); console . log ( ' Sentiment: ' , emailSentiment ); console.log('Summary:', emailSummary); console . log ( ' Summary: ' , emailSummary ); const respond = await shouldRespondToEmailAgent(emailSummary, emailSentiment); const respond = await shouldRespondToEmailAgent ( emailSummary , emailSentiment ); console.log('Respond:', respond); console . log ( ' Respond: ' , respond ); if (!respond) { if ( ! respond ) { return 'No response needed for this email.'; return ' No response needed for this email. ' ; } } const writer = await pickEmailWriterAgent(emailSummary, emailSentiment); const writer = await pickEmailWriterAgent ( emailSummary , emailSentiment ); console.log('Writer:', writer); console . log ( ' Writer: ' , writer ); const emailStream = await emailResponseAgent(writer, emailSummary); const emailStream = await emailResponseAgent ( writer , emailSummary ); const runner = getRunner(emailStream); const runner = getRunner ( emailStream ); runner.on('content', (content: string) => { runner . on ( ' content ' , ( content : string ) => { stdout.write(content); stdout . write ( content ); }); }); }; }; const userEmail = `I'm really disappointed with the service I received yesterday. The product was faulty and customer support was unhelpful.`; const userEmail = `I'm really disappointed with the service I received yesterday. The product was faulty and customer support was unhelpful.` ; const spamEmail = `Congratulations! You have been selected as the winner of a $100 million lottery!`; const spamEmail = `Congratulations! You have been selected as the winner of a $100 million lottery!` ; workflow(userEmail); workflow ( userEmail ); Copy Copied! Step #10 Step #10 # npx tsx index.ts npx tsx index.ts Copy Copied! Email: I'm really disappointed with the service I received yesterday. The product was faulty and customer support was unhelpful. Email: I'm really disappointed with the service I received yesterday. The product was faulty and customer support was unhelpful. Sentiment: frustrated Sentiment: frustrated Summary: Disappointed with faulty product and unhelpful customer support. Summary: Disappointed with faulty product and unhelpful customer support. Respond: true Respond: true Writer: formal Writer: formal Subject: Re: Concern Regarding Faulty Product and Support Experience Subject: Re: Concern Regarding Faulty Product and Support Experience Dear [User's Name], Dear [User's Name], Thank you for reaching out to us regarding your experience with our product and customer support. I sincerely apologize for the inconvenience you've encountered. Thank you for reaching out to us regarding your experience with our product and customer support. I sincerely apologize for the inconvenience you've encountered. We strive to provide high-quality products and exceptional service, and it is disappointing to hear that we did not meet those standards in your case. Please rest assured that your feedback is taken seriously, and we are committed to resolving this matter promptly. We strive to provide high-quality products and exceptional service, and it is disappointing to hear that we did not meet those standards in your case. Please rest assured that your feedback is taken seriously, and we are committed to resolving this matter promptly. To assist you further, could you please provide details about the specific issues you're facing? This will enable us to address your concerns more effectively. To assist you further, could you please provide details about the specific issues you're facing? This will enable us to address your concerns more effectively. Thank you for bringing this to our attention, and I look forward to assisting you. Thank you for bringing this to our attention, and I look forward to assisting you. Best regards, Best regards, [Your Name] [Your Name] [Your Position] [Your Position] [Company Name] [Company Name] [Contact Information] [Contact Information] Copy Copied! Follow us on X Follow us on LinkedIn Join our Discord server Follow us on GitHub State of AI AgentsExplore insights from 184B tokens and 786M agent runs by developers building AI agents on Langbase. Click here →State of AI Agents  → In this guide, you will build an AI email agent that uses multiple Langbase agent pipes to: You will build a basic Node.js application that will use the Langbase SDK to connect to the AI agent pipes and generate responses using parallelization and prompt-chaining agent architectures. There are two flows in the email agent, i.e., User Email Flow and Spam Email Flow. The User Email Flow is a normal email flow where the user sends an email, and the AI agent analyzes the email sentiment, summarizes the email content, decides the email needs a response, picks the tone of the response email, and generates the response email. The Spam Email Flow is a spam email flow where the AI agent analyzes the email sentiment, summarizes the email content, and decides that the email does not need a response. Demo workflow: Click [Send spam email] or [Send valid email] to see how it works: Let's get started! Create a new directory for your project and navigate to it. Initialize Node.js project and create an index.ts file. You will use the Langbase SDK to connect to the AI agent pipes and dotenv to manage environment variables. So, let's install these dependencies. Every request you send to Langbase needs an API key. This guide assumes you already have one. In case, you do not have an API key, please check the instructions below. Create an .env file in the root of your project and add your Langbase API key. Replace xxxxxxxxx with your Langbase API key. If you have set up LLM API keys in your profile, the Pipe will automatically use them. If not, navigate to LLM API keys page and add keys for different providers like OpenAI, TogetherAI, Anthropic, etc. Fork the following agent pipes needed for the AI email agent in Langbase dashboard: In our first step, you will analyze the email sentiment using the Langbase AI agent pipe. Go ahead and add the following code to your agents.ts file: Let's take a look at what is happening in this code: Stream the response: Do not stream: Now let's write a function in the same agents.ts file to summarize the email content. Let's break down the above code: You are building a ReAct based architecture which means the sytem first reason our the info it has and then decide to act. In this example, results of the email sentiment and summary are passed to the decision-making agent pipe to decide whether to respond to the email or not. Go ahead and add the following code to your agents.ts file: Let's go through the above code. In cases where the email needs a response, you will use the Pick Email Writer agent pipe to pick the tone of the email response. Here's what you have done in this step: Finally, you will use the Email Writer agent pipe to generate the response email based on the tone picked in the previous step. Let's take a look at the above code: Now that you have all these agents, you can combine these in multi agent workflow so they can help us In index.ts file, let's import all our agents and define a workflow that to run with user email and spam email. Here's what you have done in this step: Run the email agent workflow by running the following command in terminal: You should see the following output in the console: This is how you can build an AI email agent using Langbase agent pipes. Langbase, Inc. © Copyright 2025. All rights reserved.

Prompts

Reviews

Tags

Write Your Review

Detailed Ratings

ALL
Correctness
Helpfulness
Interesting
Upload Pictures and Videos