← Back to list

Empathetic AI with LangChain and NestJS

Feels like AI integration has become almost unavoidable in modern applications.

Saurya Paudel · 2026-05-22 05:13 · 0 claps · 4.1 min read paywalled
#empathetic-ai #langchain #langchain-tools #nestjs #llm
Open on Medium ↗
Wiki topics: LLM · Large Language Models AGT · AI Agents

Empathetic AI with LangChain and NestJS

Basic Architecture

Basic Architecture

Feels like AI integration has become almost unavoidable in modern applications.

Whether it’s:

  • AI chat assistants
  • content generation
  • recommendation systems
  • workflow automation
  • AI-powered monetization features

almost every founder, developer, and investor now wants AI integrated into their products to stay competitive.

Recently, I worked on building an empathetic AI chat system using LangChain and NestJS, and I thought it would be worthwhile to share some practical lessons from that experience.

The interesting part wasn’t just integrating an LLM. It was designing a system that could maintain conversational continuity, emotional context, and architectural flexibility while keeping costs manageable.

Why We Chose LangChain

One thing I’ve noticed in AI projects is that model selection decisions are often inconsistent.

Sometimes:

  • the technical team decides,
  • sometimes the founder chooses based on hype,
  • and other times decisions are made purely from personal preference.

The same thing happened in this project.

The founder already had preferences for certain AI models they had heard about. Instead of tightly coupling the system to a single provider too early, I suggested we experiment with multiple models and evaluate them based on:

  • user experience,
  • response quality,
  • latency,
  • and operational cost.

This is where LangChain became extremely useful.

LangChain provides a unified abstraction layer for working with multiple LLM providers such as:

  • OpenAI
  • Anthropic
  • Google

and many others.

That flexibility allowed us to switch providers without major architectural rewrites.

Initially, we started with a simpler and more cost-effective model to validate the product experience. As we gathered feedback and tested different providers, we eventually moved toward one of the more capable production-grade models that aligned better with our use case.

The Real Challenge: Building an Empathetic AI Experience

The goal of the system was not simply to generate responses.

We wanted conversations to feel:

  • context-aware,
  • emotionally aligned,
  • continuous,
  • and less robotic.

One of the biggest problems with AI chat systems is memory handling.

Without proper contextual management:

  • conversations feel disconnected,
  • responses become repetitive,
  • and the AI quickly loses continuity.

Solving this became one of the most important parts of the architecture.

1. Token Optimization and Memory Handling

A naive AI chat system continuously sends the entire conversation history to the model.

That approach becomes expensive very quickly.

Using LangChain’s memory utilities, we implemented conversational memory while optimizing token usage.

Instead of always sending full history, we:

  • trimmed older conversations based on tokens,
  • retained more relevant exchanges,
  • and selectively passed contextual information.

This significantly reduced token consumption while still maintaining conversational continuity.

const trimmedMessages = await trimMessages(allMessages, {
  maxTokens: 2000,
  strategy: 'last',
  includeSystem: true,
});

The key idea was simple:

  • Preserve full conversations in the database
  • Only send the most relevant context to the model

This helped reduce:

  • API costs,
  • latency,
  • and context overflow issues.

2. Context Engineering Beyond Default Memory

One important realization during development was this:

Conversation history alone is not enough.

Not every previous message carries equal importance.

So instead of relying entirely on LangChain’s built-in memory system, we implemented an additional contextual logic layer.

Based on:

  • previous user interactions,
  • emotional patterns,
  • and conversational history,

we selectively injected relevant contextual information into prompts before invoking the model.

This allowed the AI to:

  • respond more personally,
  • maintain emotional continuity,
  • and generate less generic replies.

In practice, contextual engineering became just as important as model selection itself.

Prompt engineering is often discussed heavily in AI applications, but contextual engineering is what actually makes long-running conversational systems feel intelligent.

3. Architecture Flexibility Matters

One thing became very obvious while working on AI integrations:

The AI landscape changes extremely fast.

The “best model” today may not be the best model six months from now.

If your system architecture is tightly coupled to a single provider, switching later becomes painful and expensive.

Using LangChain gave us the flexibility to:

  • test multiple providers,
  • compare quality and latency,
  • optimize operational costs,
  • and iterate rapidly without major backend rewrites.

That architectural abstraction ended up being one of the most valuable decisions in the project.

Technical Implementation

Supporting Multiple LLM Providers

For local experimentation and development, we integrated Ollama to run open-source models locally.

For production workloads, we used models from OpenAI.

if (useOllama) {
  this.model = new ChatOllama({
    baseUrl: process.env.OLLAMA_BASE_URL,
    model: process.env.OLLAMA_MODEL || 'llama3.2',
    temperature: 0.7,
  });
} else {
  this.model = new ChatOpenAI({
    openAIApiKey: process.env.OPENAI_API_KEY,
    modelName: process.env.OPENAI_MODEL || 'gpt-4-turbo-preview',
    temperature: 0.7,
  });
}

This setup allowed us to:

  • test models locally,
  • compare providers,
  • reduce experimentation costs,
  • and maintain portability across AI vendors.

Structuring AI Conversations

LangChain’s message abstractions helped structure conversations cleanly.

We used:

  • SystemMessage for defining AI behavior,
  • HumanMessage for user inputs,
  • AIMessage for conversation history.
const system = new SystemMessage(
  'You are an empathetic and supportive AI assistant.'
);

const human = new HumanMessage(
  'How can I manage my anxiety?'
);
const ai = new AIMessage(
  'Here are some techniques that might help...'
);

This separation made conversation orchestration significantly easier and improved maintainability.

Designing the AI Personality

We did not want the AI to behave like a generic chatbot.

Instead, we designed it around a defined behavioral framework focused on:

  • emotional validation,
  • empathetic communication,
  • non-judgmental interaction,
  • and escalation awareness for sensitive situations.
this.systemPrompt = `
You are an empathetic and supportive AI assistant
specialized in providing emotional support.
`;

One thing I learned during development is that prompting does far more than shape outputs.

It shapes the emotional experience users have with the product.

What Actually Makes an AI Product Good

A lot of people focus entirely on the model itself.

But in practice, the model is only one part of the system.

What actually defines the user experience is:

  • memory handling,
  • contextual awareness,
  • prompt engineering,
  • latency,
  • token optimization,
  • orchestration logic,
  • and architectural flexibility.

Frameworks like LangChain make experimentation easier, but the real value comes from how intelligently the surrounding system is designed.

Final Thoughts

AI integration has become more accessible than ever.

But building a genuinely good AI product still requires thoughtful engineering decisions.

The difference between a basic chatbot and a high-quality AI experience usually comes down to:

  • context management,
  • personalization,
  • memory architecture,
  • and how well the system orchestrates information around the model.

The model generates the response.

The system design defines the experience.


메타데이터
post_id
d7cf0a0a7f5e
slug
empathetic-ai-with-langchain-and-nestjs-d7cf0a0a7f5e
url
https://medium.com/@sauryap/empathetic-ai-with-langchain-and-nestjs-d7cf0a0a7f5e
canonical_url
https://medium.com/@sauryap/empathetic-ai-with-langchain-and-nestjs-d7cf0a0a7f5e
author_url
https://medium.com/@sauryap
status
ok
fetched_at
2026-07-28 15:41:11