machine-learning5 min read

ChatGPT Tutorial: Learn LLM from Scratch (2026)

ChatGPT Tutorial: Learn LLM from Scratch (2026)

Published:  |  Category: Machine Learning  |  Reading time: ~15 min
ChatGPT Tutorial: Learn LLM from Scratch (2026)

When ChatGPT launched in late 2022, it felt like science fiction had become reality. By 2026, large language models have become an everyday tool for developers, writers, and businesses. Learning to work with the OpenAI API effectively is a superpower in the modern AI landscape. I have built everything from customer support bots to code review assistants using ChatGPT's API, and the key insight I have learned is that the quality of your output depends almost entirely on the quality of your input.

API Setup and Authentication

Before you can make your first API call, you need an OpenAI account and an API key. The OpenAI dashboard lets you create and manage keys, set usage limits, and monitor consumption. Store your API key as an environment variable never hardcode it in your source code.

I once accidentally committed an API key to a public repository. Within 30 minutes, someone had scraped it and generated thousands of dollars in usage. Since then I always use a .env file and load keys with python-dotenv. The OpenAI Python client library handles authentication automatically when you set the OPENAI_API_KEY environment variable.

from openai import OpenAI
import os
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{'role': 'user', 'content': 'Hello'}])

Understanding the Chat Completions API

The Chat Completions endpoint is the core of the OpenAI API. You send a list of messages with roles: system, user, and assistant. The system message sets the behavior of the assistant, user messages are the input, and assistant messages are the model's responses. Including previous turns in the messages list enables multi-turn conversations.

The temperature parameter controls randomness: lower values like 0.2 produce focused, deterministic outputs while higher values like 0.8 produce more creative responses. Max_tokens limits the response length. I always set temperature to 0 for factual tasks like data extraction and 0.7 for creative writing.

response = client.chat.completions.create(
    model='gpt-4o',
    temperature=0.7,
    max_tokens=500,
    messages=[
        {'role': 'system', 'content': 'You are a helpful tutor.'},
        {'role': 'user', 'content': 'Explain quantum computing.'}
    ])

System Prompts and Role Setting

The system message is your most powerful tool for controlling model behavior. A well-crafted system prompt can transform the model from a general assistant into a domain expert, a code reviewer, a creative writer, or anything you need. Be specific about the persona, tone, constraints, and output format.

I built a legal document analyzer by giving the system prompt detailed instructions about legal terminology, citation format, and the jurisdiction context. The difference in output quality compared to a generic system prompt was night and day. Take the time to iterate on your system prompt it is the most important part of your integration.

system_prompt = """You are an expert Python code reviewer.
Analyze the code for bugs, style issues, and performance problems.
Provide specific line-level feedback."""
response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{'role': 'system', 'content': system_prompt},
              {'role': 'user', 'content': code_snippet}])

Function Calling for Structured Output

Sometimes you need the model to return structured data instead of free text. Function calling lets you define JSON schemas that the model can populate. The model does not execute the function instead it returns a JSON object matching your schema. This is invaluable for extracting information, generating structured reports, or powering workflows.

I replaced a complex regex-based email parser with a function call that extracts sender, subject, body, and action items as structured JSON. The accuracy jumped from 60% to 95%, and the code became trivial. The model handles variations in email formatting that would require thousands of regex patterns.

tools = [{'type': 'function', 'function': {
    'name': 'extract_info',
    'parameters': {'type': 'object', 'properties': {
        'name': {'type': 'string'},
        'amount': {'type': 'number'}
    }}
}}]
response = client.chat.completions.create(model='gpt-4o', messages=messages, tools=tools)

Streaming Responses for Real-Time UX

For chat applications, waiting for the entire response before displaying anything creates a poor user experience. Streaming lets you receive the response token by token as the model generates it. The OpenAI API supports Server-Sent Events for streaming. You set stream=True and iterate over the response chunks.

Building a real-time chatbot that streams tokens was one of my favorite projects. The user sees the model thinking in real time, which makes the interaction feel natural and responsive. You can also process each token as it arrives for things like real-time translation or sentiment analysis.

stream = client.chat.completions.create(
    model='gpt-4o',
    messages=[{'role': 'user', 'content': 'Tell me a story'}],
    stream=True)
for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end='')

Best Practices for Cost and Performance

API costs can add up quickly if you are not careful. Use the cheapest model that meets your quality requirements. GPT-4o-mini costs a fraction of GPT-4o and is sufficient for most tasks. Implement caching for repeated queries, use token limits to prevent runaway responses, and batch non-urgent requests during off-peak hours.

One optimization that saved my team 40% on API costs was implementing semantic caching. We stored embeddings of common queries and returned cached responses when similar queries came in. Only novel queries hit the API. Combined with prompt compression techniques that reduce token count by 30%, these optimizations make LLM integration economically viable at scale.

# Use the mini model for simple tasks
client.chat.completions.create(model='gpt-4o-mini', messages=messages)
# Use token limits to control cost
client.chat.completions.create(model='gpt-4o', max_tokens=200, messages=messages)

Frequently Asked Questions

How do I get an OpenAI API key?

Sign up at platform.openai.com, navigate to the API keys section in your dashboard, and create a new secret key. Store it securely as an environment variable. New accounts receive free credits to start experimenting.

What is the difference between GPT-4o and GPT-4o-mini?

GPT-4o is the flagship model with the highest intelligence and capability. GPT-4o-mini is a smaller, faster, and much cheaper model that performs well on simpler tasks. Use GPT-4o-mini for straightforward queries and GPT-4o for complex reasoning.

How do I handle rate limits with the OpenAI API?

The API returns rate limit headers. Implement exponential backoff with jitter using the tenacity library or the built-in retry mechanism in the OpenAI Python client. Consider upgrading your tier if you consistently hit limits.

Can I fine-tune ChatGPT models on my own data?

Yes, OpenAI offers fine-tuning for GPT-4o and GPT-4o-mini. You provide a training dataset of example conversations, and the model adapts to your domain. Fine-tuning is especially useful for customizing tone, style, or domain knowledge.

Originally published on Ayodhyyya. Last updated June 1, 2026.