AI Development

Building LLM-Powered Apps with LangChain

Hanabi Technologies
August 3, 2025
4 min read
Building LLM-Powered Apps with LangChain

Large Language Models (LLMs) have opened the door to powerful text‑based capabilities: natural language understanding, summarization, code generation, and more. LangChain streamlines the development of LLM applications by providing composable building blocks for prompts, chains, memory, retrieval, and agents. Whether you are prototyping or deploying production‑grade systems, LangChain helps keep your code organized, testable, and scalable.

1. Getting Started

Installation

Python

1pip install langchain openai

Node.js / TypeScript

1npm install langchain openai

Security note: Store API keys in environment variables and never commit them to source control.

2. Core Concepts

  • LLM Wrappers — unified interfaces for OpenAI, Anthropic, and other providers.
  • Prompt Templates — parameterized prompts for consistent, testable outputs.
  • Chains — pipes that connect prompts, LLM calls, parsers, and custom logic.
  • Memory — context persistence across turns.
  • Retrieval — vector stores and tools for grounding LLM output in external data.
  • Agents — decision‑making loops that select and run tools based on natural language instructions.

3. Simple Prompt and Chain (TypeScript)

Here is a basic example that uses strong typing and error handling to tell a joke:

1import { ChatOpenAI } from "langchain/chat_models/openai";
2import { PromptTemplate } from "langchain/prompts";
3import { LLMChain } from "langchain/chains";
4
5const model = new ChatOpenAI({
6 apiKey: process.env.OPENAI_API_KEY,
7 modelName: "gpt-4",
8 temperature: 0.2,
9});
10
11interface JokeInput {
12 topic: string;
13}
14
15const prompt = new PromptTemplate<JokeInput>({
16 template: "Tell a short, G-rated joke about {topic}.",
17 inputVariables: ["topic"],
18});
19
20const chain = new LLMChain({ llm: model, prompt });
21
22(async () => {
23 try {
24 const result = await chain.call({ topic: "space exploration" });
25 console.log(result.text);
26 } catch (error) {
27 console.error("Chain error:", error);
28 }
29})();

Key Points

  • Strong typing makes contracts explicit.
  • Environment variables keep secrets out of code.
  • Defensive error handling prevents silent failures.

4. Adding Memory (Python)

Memory enables continuity across multiple prompts:

1from langchain.chat_models import ChatOpenAI
2from langchain.prompts import ChatPromptTemplate
3from langchain.chains import ConversationChain
4from langchain.memory import ConversationBufferMemory
5import os
6
7llm = ChatOpenAI(
8 openai_api_key=os.environ["OPENAI_API_KEY"],
9 model_name="gpt-4",
10 temperature=0.3
11)
12
13memory = ConversationBufferMemory()
14prompt = ChatPromptTemplate.from_template(
15 "The user said: {input}\nRespond in one sentence."
16)
17
18conversation = ConversationChain(
19 llm=llm,
20 prompt=prompt,
21 memory=memory
22)
23
24print(conversation.predict(input="Hello, who are you?"))
25print(conversation.predict(input="What did I just ask?"))

Why Memory Matters: it ensures continuity in multi‑turn conversations and enables higher‑level features like chatbots or task‑oriented agents.

5. Retrieval‑Augmented Generation (TypeScript)

Retrieval helps ground model output on your own data:

1import { OpenAIEmbeddings } from "langchain/embeddings/openai";
2import { MemoryVectorStore } from "langchain/vectorstores/memory";
3import { RetrievalQAChain } from "langchain/chains";
4
5const embeddings = new OpenAIEmbeddings({ apiKey: process.env.OPENAI_API_KEY });
6
7const vectorStore = await MemoryVectorStore.fromTexts(
8 [
9 "LangChain simplifies building language model applications.",
10 "Google Docs can be integrated into productivity workflows.",
11 ],
12 ["langchain", "integration"],
13 embeddings
14);
15
16const chain = RetrievalQAChain.fromLLM(
17 new ChatOpenAI({ apiKey: process.env.OPENAI_API_KEY, modelName: "gpt-4" }),
18 vectorStore.asRetriever()
19);
20
21const response = await chain.call({ query: "What is LangChain?" });
22console.log(response.text);

Best Practices

  • Cache embeddings or persist vector stores for production performance.
  • Validate retriever responses to avoid hallucinated or outdated data.

6. Tool‑Using Agents (Python)

Agents can decide which tools to use based on natural language input. Here's a minimal example with a weather API:

1from langchain.agents import initialize_agent, AgentType, Tool
2from langchain.chat_models import ChatOpenAI
3import requests, os
4
5def get_current_weather(city: str) -> str:
6 response = requests.get(
7 f"https://wttr.in/{city}",
8 params={"format": "3"},
9 timeout=5,
10 )
11 return response.text
12
13weather_tool = Tool(
14 name="weather",
15 func=get_current_weather,
16 description="Get current weather for a city",
17)
18
19llm = ChatOpenAI(
20 openai_api_key=os.environ["OPENAI_API_KEY"],
21 model_name="gpt-4",
22 temperature=0
23)
24
25agent = initialize_agent(
26 [weather_tool],
27 llm,
28 agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
29 verbose=True,
30)
31
32print(agent.run("What's the weather like in Tokyo right now?"))

Guidelines

  • Tools should validate inputs and handle network failures gracefully.
  • Use timeouts and logging around external calls to avoid deadlocks and silent errors.

7. Deployment Tips

  • Environment management: Use Docker or virtual environments for consistent builds.
  • Monitoring: Log inputs/outputs (without sensitive data) and track latency or token usage.
  • Testing: Mock LLM calls to keep tests deterministic and affordable.
  • Scaling: Pre‑warm vector stores, handle rate limits, and track usage quotas.
  • Safety: Sanitize user input, add content filters, and handle unexpected LLM responses.

8. Where LangChain Fits in a Team

While this article focuses on building your own pipelines, it's worth noting that production AI assistants — like chatbots and productivity aids — use similar patterns internally, just like Hana. The key takeaways are: design modular components, separate prompts from logic, persist context when needed, and ground the model in trustworthy data. These practices ensure your LLM apps remain maintainable, scalable, and safe.

Conclusion

LangChain provides a robust foundation for building LLM‑powered applications that are maintainable, scalable, and secure. With a few well‑typed modules, you can compose complex pipelines, integrate external data, and deploy sophisticated agents. Whether you're building internal tools or launching customer‑facing products, LangChain and the best practices above will help you deliver reliable, high‑impact experiences.

More insights

Explore more articles from our AI and development experts