Every LangChain lesson assumes you know what an LLM does, what you send it and what it costs; this is that picture.
You have probably used ChatGPT or Claude: you type a question and get a written answer. Behind those apps is a large language model (LLM): a program trained on huge amounts of text to continue text the way a person might. LLMs are one kind of artificial intelligence (AI), software that does tasks we used to think needed a human mind.
The text you send a model is the prompt. Today's LLMs are mostly chat models: the prompt is a short conversation made of messages, each marked with who wrote it (the user, the model, or instructions from your app), and the model writes the next message.
Companies such as OpenAI, Anthropic and Google run their models on their own computers. Your program uses one through an API (application programming interface): a fixed set of your code can send over the internet, with answers coming back. Each company offering models is called a provider.
To use a provider's API you need an API key: a long code that identifies you, so the provider knows whom to bill. Never write it into your code. Put it in an environment variable, a named setting your system hands to every program it starts, and libraries read it from there.
Models read and write text in tokens: chunks of a few characters, roughly three quarters of an English word on average. Providers charge per token. Every model also has a context window: the most tokens it can handle at once, prompt and reply together.
Two habits of LLMs matter for programmers. Their answers vary: asking twice can give different wording, so the output is not deterministic. And a model can state false things with full confidence, which is called hallucination, so check facts that matter.
LangChain is a Python library that lets your code use models from any provider in the same way, and adds building blocks for prompts, multi-step programs and models that can call your own functions. The next lesson maps its packages.
Example
import os
# Set the key once in your terminal, not in code:
# export OPENAI_API_KEY="sk-..." (macOS/Linux)
# setx OPENAI_API_KEY "sk-..." (Windows)
key = os.environ.get("OPENAI_API_KEY")
print("key found" if key else "set OPENAI_API_KEY first")
# A chat prompt is a list of messages, each with a role:
prompt = [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Name three fruits."},
]
print(len(prompt), "messages") # 2 messages
# The model's reply would be the next message, e.g.
# {"role": "assistant", "content": "Apple, mango, pear."}Remember
- An LLM writes text continuing a prompt; chat models take a list of messages
- Programs reach a provider's model through its API, identified by a secret API key kept in an environment variable
- Usage is counted in tokens, limited by the context window
- Answers vary between calls and can be confidently wrong
Watch out for
- Pasting an API key into your code, where it ends up shared or published with the code.
New words in this lesson
- request
- A message a client sends to a server asking it to do something, such as return a page or save data.
- secret
- Sensitive data such as passwords, API keys or tokens that must not be shared or stored in code.