Prompts, LLMs, and Basic Chains
Master the art of prompt engineering, connect to various LLM providers, and create simple sequential chains for basic tasks.
Prompts, LLMs, and Basic Chains is a free LangChain / RAG / Vector DBs lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the LangChain / RAG / Vector DBs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Prompts, LLMs, & Chains
Welcome to this lesson! We'll explore the fundamental building blocks of LangChain: Prompts, Large Language Models (LLMs), and Chains.
These three components are at the heart of almost every application you'll build with LangChain, enabling powerful interactions with AI.
The Art of Prompt Engineering
A prompt is the input text you give to an LLM to guide its response. Crafting effective prompts is known as prompt engineering.
Good prompts are clear, concise, and provide enough context for the LLM to generate the desired output. They are crucial for getting useful results.
Dynamic Prompts with Templates
Instead of hardcoding prompts, LangChain uses PromptTemplate to create dynamic prompts. This allows you to insert variables into your prompt text.
Here's a simple example of how to define a template and format it:
from langchain_core.prompts import PromptTemplate
def main():
template = "What is a good name for a company that makes {product}?"
prompt = PromptTemplate.from_template(template)
# Format the prompt with a specific product
formatted_prompt = prompt.format(product="colorful socks")
print(formatted_prompt)
if __name__ == "__main__":
main()Connecting to LLMs
LangChain provides a unified interface to interact with various Large Language Models (LLMs), such as OpenAI's GPT series or Google's Gemini.
You typically need an API key from your chosen provider. LangChain abstracts away the specifics, letting you swap models easily.
Making Your First LLM Call
Let's see how to connect to an LLM and make a simple call. We'll use ChatOpenAI as a common example, but the pattern is similar for others.
Remember to set your API key as an environment variable (OPENAI_API_KEY).
import os
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
def main():
# Make sure your OPENAI_API_KEY is set as an environment variable
# os.environ["OPENAI_API_KEY"] = "your_api_key_here"
if "OPENAI_API_KEY" not in os.environ:
print("Please set the OPENAI_API_KEY environment variable.")
return
llm = ChatOpenAI(temperature=0.7)
# Invoke the LLM with a simple message
response = llm.invoke([HumanMessage(content="Tell me a short, funny story.")])
print(response.content)
if __name__ == "__main__":
main()What are LangChain Chains?
Chains are a core concept in LangChain. They allow you to combine LLMs with other components, or even other chains, into multi-step workflows.
Instead of making individual LLM calls, chains let you define a sequence of operations, making your applications more structured and powerful.
The Simple LLMChain
The LLMChain is one of the simplest and most fundamental chains. It combines a PromptTemplate and an LLM (or ChatModel) into a single, executable unit.
It takes input variables, formats them into the prompt, sends the prompt to the LLM, and returns the LLM's response.
Building Your First LLMChain
Let's create an LLMChain to generate company names based on a product description. We'll use the prompt template and LLM we discussed.
This shows how easy it is to link a prompt and an LLM together.
import os
from langchain_openai import ChatOpenAI
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
def main():
# Set your API key
if "OPENAI_API_KEY" not in os.environ:
print("Please set the OPENAI_API_KEY environment variable.")
return
# 1. Define the PromptTemplate
prompt_template = PromptTemplate.from_template(
"What is a creative name for a company that makes {product}?"
)
# 2. Initialize the LLM
llm = ChatOpenAI(temperature=0.7)
# 3. Create the LLMChain
chain = LLMChain(llm=llm, prompt=prompt_template)
# 4. Run the chain with an input
response = chain.invoke({"product": "eco-friendly water bottles"})
print(response["text"])
if __name__ == "__main__":
main()Chaining Multiple Inputs
An LLMChain can handle multiple input variables in its prompt template, making it highly flexible. Just ensure all variables are provided when invoking the chain.
Here's an example with two inputs: product and style.
import os
from langchain_openai import ChatOpenAI
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
def main():
# Set your API key
if "OPENAI_API_KEY" not in os.environ:
print("Please set the OPENAI_API_KEY environment variable.")
return
# Define a prompt with multiple input variables
prompt_template = PromptTemplate.from_template(
"Suggest {num} {style} names for a company that sells {product}."
)
llm = ChatOpenAI(temperature=0.8)
chain = LLMChain(llm=llm, prompt=prompt_template)
# Invoke the chain with all required inputs
response = chain.invoke({
"num": 3,
"style": "modern",
"product": "handmade jewelry"
})
print(response["text"])
if __name__ == "__main__":
main()Quick Check
You've learned about prompts, LLMs, and basic chains. Let's test your understanding of how these pieces fit together in LangChain.
Recap & Next Steps
Great job! In this lesson, you've mastered the fundamentals:
- Prompt Engineering: How to craft effective inputs for LLMs.
- LLM Integration: Connecting to and making calls with Large Language Models using LangChain.
- Basic Chains: Understanding and building your first
LLMChainto sequence prompts and LLMs.
These skills are essential for building more complex applications. Next, we'll dive into Output Parsers and Callbacks to further control and monitor your LangChain applications.
Frequently asked questions
Is the “Prompts, LLMs, and Basic Chains” lesson free?
Yes — the full text of “Prompts, LLMs, and Basic Chains” is free to read here on the web, and the LangChain / RAG / Vector DBs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the LangChain / RAG / Vector DBs course, upgrade to CoddyKit PRO.
What will I learn in “Prompts, LLMs, and Basic Chains”?
Master the art of prompt engineering, connect to various LLM providers, and create simple sequential chains for basic tasks. You practise LangChain / RAG / Vector DBs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start LangChain / RAG / Vector DBs?
No prior experience is required. LangChain / RAG / Vector DBs on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Prompts, LLMs, and Basic Chains” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this LangChain / RAG / Vector DBs lesson?
Yes. Every LangChain / RAG / Vector DBs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up Your LangChain Environment
- Prompts, LLMs, and Basic Chains
- Output Parsers and Callbacks
- Memory and Conversational Context in LangChain