Generative AI

Day 1: Your First LLM Call with LangChain

Start the 50 Days of Generative AI challenge with Code With Sri. Set up Python and VS Code, make your first LLM call with LangChain, and understand every line.

Diagram for Day 1: Your First LLM Call with LangChain
On this page

This is Day 1 of the Code With Sri 50-Day Gen AI Learning Challenge. In this guide, you will send a prompt to a large language model using Python and LangChain, then display its answer in a VS Code notebook.

Our prompt will be:

Explain API using a restaurant analogy.

Understand the basics

A Large Language Model, or LLM, is an AI model that generates responses based on the input it receives. You can use it to answer questions, summarize text, and explain concepts.

LangChain is a framework for building applications with language models. We will use it to choose a model, send a prompt, and receive an answer.

A prompt is the input you give the model. In this example, we ask it to explain an API through a familiar situation: ordering food at a restaurant.

1. Prerequisites

You will need:

  • Python 3.10 or later installed on your computer.
  • VS Code, with Microsoft's Python and Jupyter extensions installed.
  • An OpenAI API key, with available API quota and access to the model you use.
  • An internet connection to send the request.

Basic familiarity with Python imports, variables, and function calls is enough to follow along.

2. Create your project

Create a folder named genai-50-days-learning-challenge and open it in VS Code using File → Open Folder.

Open a terminal using Terminal → New Terminal. Install the packages:

bash
python -m pip install langchain langchain-openai python-dotenv ipykernel

Use py on Windows or python3 on macOS/Linux if that is how you run your installed Python.

PackagePurpose
langchainProvides the function used to initialize our model
langchain-openaiConnects LangChain to OpenAI models
python-dotenvLoads the API key from a .env file
ipykernelRuns Python code in a Jupyter notebook

3. Add your API key

Create an API key in your OpenAI API account.

Inside the project folder, create a file named .env:

dotenv
OPENAI_API_KEY=replace_with_your_actual_api_key

Replace the placeholder with your API key. This keeps the key separate from the Python code you will share or show in your video.

If you use Git, create a .gitignore file and add:

gitignore
.env
__pycache__/
.ipynb_checkpoints/

4. Create a notebook in VS Code

  1. Create a file named day-01-first-llm-call.ipynb inside your project folder.
  2. Open the notebook and click Select Kernel in the top-right corner.
  3. Select the Python interpreter where you installed the packages.
  4. Add a Code cell.

Keep the notebook and .env file in the same project folder.

5. Make your first LLM call

Paste this program into the code cell:

python
from dotenv import load_dotenv
 
load_dotenv()
 
from langchain.chat_models import init_chat_model
 
model = init_chat_model(model="gpt-5-nano")
 
response = model.invoke("Explain API using a restaurant analogy.")
 
print(response.content)

Click the cell's Run button or press Shift + Enter. The answer will appear below the cell.

Each time you run the cell, it sends a new request to the model and may incur API usage charges. Use a model available to your API project; if needed, replace the model name in init_chat_model.

6. Understand every line

Load the API key

python
from dotenv import load_dotenv
 
load_dotenv()

We import load_dotenv from the python-dotenv package and call it to load settings from .env. The OpenAI integration uses the OPENAI_API_KEY value to authenticate our request.

Import the model initializer

python
from langchain.chat_models import init_chat_model

init_chat_model is the LangChain function we use to set up a chat-model client.

Choose a model

python
model = init_chat_model(model="gpt-5-nano")

We choose the model and store the client in a variable named model. At this point, we have prepared the client but have not sent our question.

The model runs on the provider's servers, so we do not download it to our computer.

Send the prompt

python
response = model.invoke("Explain API using a restaurant analogy.")

invoke() sends our prompt to the model and waits for its answer. We store the returned message in response.

The prompt tells the model both what to explain—an API—and how to explain it—using a restaurant analogy.

Display the answer

python
print(response.content)

.content gives us the reply's content, and print() displays it below the notebook cell.

Optional: run it as a Python script

You can also save the same program in a file named first_llm_call.py, alongside .env.

Run it from your project terminal:

bash
python first_llm_call.py

The answer will appear in the terminal instead of below a notebook cell.

What you learned

You have made your first LLM call from Python:

  • load_dotenv() loads your API key from .env.
  • init_chat_model() prepares the model client.
  • invoke() sends your prompt and returns a message.
  • print(response.content) displays the answer.

Try it yourself

Keep the program the same and replace the prompt with one of these:

  • Suggest a weekend itinerary for visiting Kyoto.
  • Write a short poem about the first rain of summer.
  • What happened to NVIDIA CEO

Compare the answers. Did the model follow the requested length? Was the explanation understandable? Did it make claims you should verify?

Keep learning with Sri

More practical tutorials and experiments on the channel.

Watch on YouTube
Back to articles