Artificial Intelligence: How AI Works and How to Create Your Own AI Model

Artificial Intelligence How AI Works

Artificial intelligence (AI) is the field of computer science that builds machines able to do things we normally associate with human intelligence: understanding language, recognising images, solving problems, making decisions and learning from experience. John McCarthy, who coined the term in 1955, defined it as “the science and engineering of making intelligent machines”.

In 2026 AI is no longer a research curiosity. It writes and translates text, generates images and video, writes code, drives cars in several cities, discovers new materials and drugs, and is used by most companies in the world. This guide explains what artificial intelligence is, how it works, the main types of AI, how it developed from Alan Turing to ChatGPT and AI agents, and what its risks and limits are. To make the ideas concrete, we also built two tiny AIs in Python, one that reasons by search and one that learns from experience, and measured how they play. Finally, a hands-on section shows how to create your own AI model: where to get an architecture, how to prepare your data, and a short script that trains a small language model from scratch.

Key takeaways

  • AI is the broad field of making computers perform tasks that require intelligence. Machine learning is the main way AI is built today, and deep learning and generative AI are subsets of it.
  • Two approaches: classic (symbolic) AI follows rules and searches through possibilities written by people; modern AI learns patterns from data.
  • All AI in use today is narrow AI: it is very good at specific tasks. Artificial general intelligence (AGI), which would match humans across all tasks, does not yet exist, though today’s models handle a far wider range of tasks than any before.
  • In our experiment, a short Python program learned tic-tac-toe by playing 20,000 games against a random opponent, cutting its losses from 14% to almost zero, and then drew every game against a perfect player.
  • To create your own AI model, you rarely need to start from zero: take a ready-made architecture from Hugging Face Transformers and your own data. Our script trained a 5.5-million-parameter language model on Shakespeare in 12 minutes on two CPU cores, without a GPU.
  • In 2026, 88% of organisations use AI, the EU AI Act is partly in force, and the main concerns are errors, bias, misuse, jobs and energy use.

What is artificial intelligence?

There is no single agreed definition, because there is no single agreed definition of intelligence. McCarthy described intelligence as the computational part of the ability to achieve goals in the world, and noted that people, many animals and some machines have it in different kinds and degrees. A practical working definition: artificial intelligence is any technique that lets a computer perform a task which, if a person did it, we would say requires intelligence.

Three clarifications help:

  • AI does not have to copy the human brain. Sometimes researchers borrow ideas from biology, as with neural networks, but often they use methods no human uses, such as checking millions of chess positions per second.
  • Intelligence is not all or nothing. A program can be superhuman at one task and useless at another. A chess engine that beats every grandmaster cannot hold a conversation, and a chatbot that writes essays may fail at counting letters in a word.
  • The goalposts move. Once a problem is solved, people tend to stop calling it AI. Route planning, spam filters and optical character recognition were once AI research; today they are just software. This is known as the “AI effect”.

AI is also not the same as machine learning. AI is the goal; machine learning is the most successful method of reaching it today:

Term What it is Example
Artificial intelligence The whole field: any method that makes computers act intelligently, including hand-written rules and search A chess engine, a route planner, a chatbot
Machine learning AI that learns its behaviour from data instead of rules A spam filter, product recommendations
Deep learning Machine learning with many-layered neural networks Face recognition, speech recognition, translation
Generative AI Deep learning models that create text, images, audio, video or code ChatGPT, Claude, Gemini, image generators

How does AI work?

Over seventy years, two broad approaches have competed.

Symbolic AI: rules, logic and search

The first generation of AI, later nicknamed “good old-fashioned AI”, represented knowledge as symbols and rules written by people, and solved problems by logical reasoning and by searching through possible actions. Chess programs, route planners, theorem provers and the “expert systems” of the 1980s, which encoded the knowledge of doctors or engineers as thousands of if-then rules, all work this way. Symbolic AI is precise and explainable, but it breaks down when the rules are too many or too fuzzy to write down, as with recognising a face or understanding a casual sentence.

Machine learning: learning from data

Instead of writing rules, you give the computer examples and let it find the patterns. A learning algorithm adjusts the parameters of a model to reduce its errors on the training data, usually with gradient descent. Since the 2010s, deep learning, which uses artificial neural networks with many layers trained by backpropagation, has driven almost all progress: convolutional networks for images, recurrent networks and then transformers for language.

Foundation models: how modern AI is built

Today’s most capable systems, such as large language models, are foundation models: one large neural network trained on broad data and then adapted to many tasks. Every such model starts life as a file of random numbers that produces gibberish. Turning it into a useful assistant takes a team of researchers and engineers, months of work and thousands of GPUs. The big labs keep their exact recipes secret, but open projects publish theirs. A good example is OLMo 3, released by the Allen Institute for AI (Ai2) in November 2025 with all its data, code and intermediate checkpoints, and the steps below follow the pattern that it, Meta’s Llama and DeepSeek have described.

  1. Collect and clean the data. The raw material is text and code: web pages from crawls such as Common Crawl, books, scientific papers, encyclopedias and public code repositories, often with images, audio and video for multimodal models. Most of the raw crawl is thrown away. Filters remove duplicates, spam, machine-generated junk, adult and toxic content and personal data; quality classifiers keep pages that look like good writing; known benchmark questions are removed so that tests stay fair. The team then decides the mix: how much web text, code, maths and each language. OLMo 3’s corpus, Dolma 3, has about 9.3 trillion tokens, from which a 5.9-trillion-token mix was used for pretraining.
  2. Turn text into tokens. A neural network works with numbers, so a tokenizer splits text into pieces: whole common words, parts of rarer words, single characters. It is usually built with byte-pair encoding, which merges the most frequent character sequences into a vocabulary of roughly 100,000 to 250,000 tokens. In English, one token is about three quarters of a word.
  3. Design the network and choose its size. Almost all large models are decoder-only transformers: a stack of dozens to over a hundred identical layers, each combining self-attention, which lets every token look at the previous ones, with a feed-forward network. The designers choose the number of layers, their width, the context length and whether to use a mixture of experts, where only part of the network is active for each token: DeepSeek-V3 has 671 billion parameters but uses 37 billion per token. To pick the size, labs train many small models first and fit scaling laws, curves that predict how the error of a large model falls with more parameters, data and compute. The 2022 Chinchilla study suggested about 20 training tokens per parameter; modern models are trained far longer than that, because a smaller model trained on more data is cheaper to run afterwards.
  4. Pretrain from random weights. The weights are initialised with small random numbers, and the network is shown text and asked to predict the next token at every position. The loss measures how surprised it was by the real token, backpropagation computes how each of the billions of weights contributed to the error, and an optimizer such as AdamW nudges them all a little. This repeats for hundreds of thousands of steps, each on a batch of millions of tokens, while the learning rate is first raised and then slowly lowered. One GPU would need centuries, so the work is split across thousands of them: each GPU processes different data, holds a slice of the layers or a part of each large matrix, and they exchange results over fast networks. Meta trained Llama 3.1 405B on more than 15 trillion tokens using up to 16,384 H100 GPUs and about 31 million GPU-hours; DeepSeek reported 14.8 trillion tokens and 2.8 million GPU-hours for DeepSeek-V3, about $5.6 million at rental prices for the final run alone. OLMo 3 was pretrained on up to 1,024 H100s. A large part of the engineering is keeping such a run alive: GPUs fail, the loss sometimes spikes, and the model is saved regularly so training can resume from the last checkpoint. You can estimate what renting such hardware costs with our guide to cloud GPU pricing.
  5. Mid-train and extend the context. Near the end of pretraining, the data mix shifts to the highest-quality sources: maths, code, textbooks, step-by-step reasoning. A short extra phase on long documents teaches the model to use long inputs. OLMo 3 spent about 100 billion tokens on mid-training and about 50 billion on long-context training, a small fraction of pretraining with a large effect on ability.
  6. Post-train it into an assistant. The result of pretraining is a base model: it continues any text plausibly, but it does not follow instructions or know when to stop. Three kinds of training follow. In supervised fine-tuning, the model learns from hundreds of thousands of example conversations with good answers, written by people or generated by stronger models. In preference training, people or AI judges compare pairs of answers, and the model is pushed towards the preferred ones with reinforcement learning from human feedback (RLHF) or a simpler method such as DPO. In reinforcement learning with verifiable rewards, the model solves maths problems whose answers can be checked and programming tasks whose code must pass tests, and is rewarded for correct results. This last step, scaled up in 2024 and 2025, produced reasoning models such as OpenAI o1 and DeepSeek-R1, which learn to think step by step before answering. Safety training, which teaches the model to refuse dangerous requests, is part of post-training too.
  7. Evaluate and test. Before release, the model is measured on benchmarks of knowledge, reasoning, coding and safety, compared with earlier versions in blind tests, and probed by red teams that try to make it misbehave. Developers of frontier models usually publish the results in a model card or system card.
  8. Deploy and use with tools. To serve millions of users, the model is often compressed, for example by quantization to fewer bits per weight, or distilled into smaller and faster models that learn to imitate it. At run time, the model can search the web, run code or call other programs. Models that plan and carry out multi-step tasks this way are called AI agents.

The cost has grown quickly. Epoch AI estimated in 2024 that the cost of the largest training runs had grown 2 to 3 times per year since 2016 and projected that it would pass a billion dollars by 2027; hardware made up roughly half to two thirds of the total, research staff most of the rest and electricity only a few percent. Final training runs are also only part of the bill: labs spend more compute on experiments than on the run that produces the released model.

Building a small model from scratch, on the other hand, is within anyone’s reach. The same steps work at toy scale: Andrej Karpathy’s open-source nanochat project trains a small ChatGPT-like model, tokenizer, pretraining and fine-tuning included, in about four hours on one rented 8-GPU server for roughly $100. Our guides to how neural networks work and the PyTorch tutorial show the basic training loop on a laptop. In practice, most companies do not pretrain at all: they take an open model and fine-tune it on their own data, which costs a tiny fraction of training from scratch. The practical path below shows where to get an architecture, how to format your data and how to train a small model yourself.

Modern systems often combine both traditions: a neural network proposes, and search or symbolic checking verifies. AlphaGo combined deep learning with tree search; reasoning models generate many candidate solutions and check them; coding agents run tests on the code they write.

Types of AI

By capability: narrow AI, AGI and superintelligence

  • Narrow AI (weak AI, ANI) is built for specific tasks: translating, recommending films, detecting fraud, driving. All AI that exists today is narrow, even when, like a modern chatbot, it covers a surprisingly wide range of tasks.
  • Artificial general intelligence (AGI, sometimes called strong AI) would match or exceed humans across practically all cognitive tasks, including learning new skills on its own. It is the stated goal of several leading AI labs. Whether and when it will arrive is one of the most debated questions in technology.
  • Artificial superintelligence (ASI) is a hypothetical AI far smarter than the best humans in every field. It exists only in theory and science fiction.

By functionality: the four-type model

A popular classification by the researcher Arend Hintze sorts AI by how it uses memory and models of the world:

  1. Reactive machines respond to the current situation without memory, like IBM’s chess computer Deep Blue.
  2. Limited memory systems use recent data and learned experience, like self-driving cars and today’s language models. Nearly all modern AI belongs here.
  3. Theory of mind AI would understand the beliefs, intentions and emotions of others. Current models imitate parts of this in conversation, but whether they truly have it is disputed.
  4. Self-aware AI would have consciousness. No such system exists.

By technique

In practice, engineers talk about the method: rule-based systems, classic machine learning (decision trees, regression, gradient boosting), deep learning, generative models (GANs, VAEs, diffusion models, large language models), reinforcement learning and AI agents. By the kind of feedback used in training, machine learning further splits into supervised and unsupervised learning and reinforcement learning.

History of artificial intelligence

Who invented AI? No single person. Alan Turing laid the foundations in a 1947 lecture and in his 1950 paper “Computing Machinery and Intelligence”. John McCarthy coined the term “artificial intelligence” in 1955 in the proposal for the Dartmouth summer workshop of 1956, which he organised with Marvin Minsky, Nathaniel Rochester and Claude Shannon and which is usually considered the birth of the field. The key milestones:

Year Milestone
1943 Warren McCulloch and Walter Pitts describe a mathematical model of a neuron
1950 Alan Turing proposes the imitation game, later called the Turing test
1956 The Dartmouth workshop gives the field its name
1958 Frank Rosenblatt builds the perceptron, a learning machine based on an artificial neuron
1966 Joseph Weizenbaum’s ELIZA, the first chatbot, imitates a psychotherapist
1970s–1980s Expert systems boom in business; two “AI winters” of disappointment and cut funding follow over-promising
1986 Backpropagation is popularised for training multi-layer neural networks
1997 IBM’s Deep Blue defeats world chess champion Garry Kasparov
2012 AlexNet wins the ImageNet competition by a wide margin, starting the deep learning era
2016 DeepMind’s AlphaGo defeats Go champion Lee Sedol
2017 Google researchers introduce the transformer architecture behind today’s language models
2022 OpenAI releases ChatGPT; generative AI reaches the mass market
2024 Nobel Prizes in physics (Hopfield, Hinton) and chemistry (Hassabis, Jumper, Baker) go to AI-related work
2025–2026 Reasoning models and AI agents that use tools and work on long tasks become mainstream

The Turing test

In 1950 Turing replaced the vague question “Can machines think?” with a practical one. In his imitation game, a judge chats in writing with a human and a machine without seeing them. If the judge cannot reliably tell which is which, the machine should be considered intelligent. Text-only conversation is used so that the machine does not have to imitate a human voice or appearance.

Voight-Kampff test scene from the film Blade Runner, a fictional test for telling humans from androids
The Voight-Kampff test in Blade Runner, a fictional cousin of the Turing test

For decades no program came close in a serious test. That changed recently. In a 2025 study at UC San Diego, Cameron Jones and Benjamin Bergen ran a pre-registered three-party Turing test: each judge chatted for five minutes with a human and an AI at the same time. GPT-4.5, prompted to adopt a humanlike persona, was picked as the human 73% of the time, more often than the real people. ELIZA from 1966 managed 23%. Researchers point out that passing the test shows how well a model imitates human conversation, not whether it understands or thinks, which is why the field now relies on a wide range of benchmarks instead.

Two ways to build an AI: search vs learning

To see the two traditions side by side, we built two tic-tac-toe players in plain Python, with no libraries.

Symbolic AI: perfect play by search

The first program knows only the rules. It uses the minimax algorithm: it imagines every possible continuation of the game, assumes the opponent always plays its best move, and chooses the move with the best guaranteed outcome. This is how classic game-playing AI works, from early chess programs to Deep Blue, which added clever shortcuts to search far deeper.

from functools import lru_cache

LINES = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)]

def winner(board):
    for a, b, c in LINES:
        if board[a] != "." and board[a] == board[b] == board[c]:
            return board[a]
    return None

@lru_cache(maxsize=None)
def minimax(board, player):
    """Score of a position for X: +1 X wins, 0 draw, -1 O wins, assuming perfect play from both sides."""
    w = winner(board)
    if w:
        return 1 if w == "X" else -1
    if "." not in board:
        return 0
    scores = []
    for i, cell in enumerate(board):
        if cell == ".":
            child = board[:i] + player + board[i + 1:]
            scores.append(minimax(child, "O" if player == "X" else "X"))
    return max(scores) if player == "X" else min(scores)

def best_move(board, player):
    moves = [i for i, c in enumerate(board) if c == "."]
    nxt = "O" if player == "X" else "X"
    score = lambda i: minimax(board[:i] + player + board[i + 1:], nxt)
    return max(moves, key=score) if player == "X" else min(moves, key=score)

if __name__ == "__main__":
    # The program plays both sides perfectly
    board, player = "." * 9, "X"
    while not winner(board) and "." in board:
        i = best_move(board, player)
        board = board[:i] + player + board[i + 1:]
        player = "O" if player == "X" else "X"
    for row in range(3):
        print(" ".join(board[row * 3:row * 3 + 3]))
    print("result:", winner(board) or "draw")
    print("value of the empty board:", minimax("." * 9, "X"))
    print("distinct positions evaluated:", minimax.cache_info().currsize)
X X O
O O X
X O X
result: draw
value of the empty board: 0
distinct positions evaluated: 5478

The program examines all 5,478 legal positions of tic-tac-toe and proves that the game is a draw with perfect play: the empty board has a value of 0. It never loses, but it learned nothing; its strength comes entirely from exhaustive search. The same approach fails for Go, which has more legal positions than there are atoms in the observable universe.

Machine learning: improving from experience

The second program does not search. It starts knowing nothing about which positions are good and plays thousands of games against an opponent that moves at random. After each move it updates a table of position values, nudging the estimate for its previous position towards the value of its new one. This is temporal-difference learning, a form of reinforcement learning described in Richard Sutton and Andrew Barto’s classic textbook. It only learns when a game ends that wins are good and losses are bad, and this signal gradually flows back to the earlier moves that led there. The script reuses the rules from the first program, so save that one as minimax.py in the same folder.

import json
import random
from minimax import winner, best_move

random.seed(0)
V = {}                       # learned value of a position for X: 1 = sure win, 0 = sure loss
ALPHA, EPSILON = 0.2, 0.1

def value(board):
    w = winner(board)
    if w:
        return 1.0 if w == "X" else 0.0
    if "." not in board:
        return 0.5
    return V.get(board, 0.5)                               # unknown positions start at 0.5

def play(opponent, learn=True):
    """One game: the learning agent is X, the opponent is O. Returns 'X', 'O' or 'draw'."""
    board, prev = "." * 9, None
    while True:
        moves = [i for i, c in enumerate(board) if c == "."]
        if learn and random.random() < EPSILON:
            i = random.choice(moves)                       # explore: try a random move
        else:
            best = max(value(board[:m] + "X" + board[m + 1:]) for m in moves)
            i = random.choice([m for m in moves if value(board[:m] + "X" + board[m + 1:]) == best])
        board = board[:i] + "X" + board[i + 1:]
        if learn and prev is not None:                     # move the old estimate towards the new one
            V[prev] = value(prev) + ALPHA * (value(board) - value(prev))
        prev = board
        if not winner(board) and "." in board:             # the opponent replies
            j = opponent(board)
            board = board[:j] + "O" + board[j + 1:]
        if winner(board) or "." not in board:
            if learn:
                V[prev] = value(prev) + ALPHA * (value(board) - value(prev))
            return winner(board) or "draw"

random_player = lambda b: random.choice([i for i, c in enumerate(b) if c == "."])
perfect_player = lambda b: best_move(b, "O")

def evaluate(opponent, games=1000):
    results = [play(opponent, learn=False) for _ in range(games)]
    return {r: results.count(r) / games for r in ("X", "draw", "O")}

history = []
for games in range(0, 20001, 2000):
    if games:
        for _ in range(2000):
            play(random_player)
    r = evaluate(random_player)
    history.append((games, r["X"], r["draw"], r["O"]))
    print(f"after {games:5d} games: win {r['X']:.1%}  draw {r['draw']:.1%}  loss {r['O']:.1%}")
print("positions learned:", len(V))
print("vs a perfect player:", evaluate(perfect_player, 100))
json.dump(history, open("learn.json", "w"))
after     0 games: win 79.2%  draw 6.9%  loss 13.9%
after  2000 games: win 96.3%  draw 2.2%  loss 1.5%
after  4000 games: win 97.3%  draw 1.8%  loss 0.9%
after  6000 games: win 98.1%  draw 1.0%  loss 0.9%
after  8000 games: win 97.7%  draw 2.1%  loss 0.2%
after 10000 games: win 97.7%  draw 1.5%  loss 0.8%
after 12000 games: win 98.9%  draw 1.1%  loss 0.0%
after 14000 games: win 98.3%  draw 1.6%  loss 0.1%
after 16000 games: win 98.0%  draw 2.0%  loss 0.0%
after 18000 games: win 99.0%  draw 1.0%  loss 0.0%
after 20000 games: win 99.2%  draw 0.5%  loss 0.3%
positions learned: 1540
vs a perfect player: {'X': 0.0, 'draw': 1.0, 'O': 0.0}
A reinforcement learning agent learns tic-tac-toe: win rate against a random opponent rises from 79% to 99% and loss rate falls from 14% to 0.3% over 20,000 training games
Learning from experience: the agent stops losing within a few thousand games

Before training, the agent already wins 79% of games against a random opponent, simply because it takes a winning move when it sees one, but it loses 14% of games. After 2,000 practice games the loss rate falls to 1.5%, and after 20,000 games the agent wins 99% of games and almost never loses. It learned values for 1,540 positions. Most strikingly, it then drew all 100 games against the perfect minimax player, an opponent it had never met: it learned to defend from experience alone.

The two programs show the trade-off that shaped AI history. Search is exact but needs a model of the world and becomes impossible when the number of possibilities explodes. Learning needs lots of experience but generalises to situations that nobody wrote rules for. AlphaGo combined both: a neural network learned which moves and positions look promising, and search looked ahead from there.

How to create your own AI model: a practical path

The foundation-model recipe described earlier is what large labs do. If you have collected your own data and want a model of your own, these are the practical next steps: decide whether you need to train at all, take a ready-made architecture, put the data in the format the training code expects, and start small.

First decide whether you need to train a model

Training from scratch is the most expensive option and rarely the best one. Choose by goal:

Your goal Best approach What you need
A chatbot that answers questions about your documents, products or knowledge base Retrieval-augmented generation (RAG): an existing model plus search over your files; no training Your documents split into chunks, an embedding model and a vector database
A model that writes in your style or format, or does one narrow task well Fine-tuning an open model such as Llama, Qwen, Gemma or OLMo, usually with LoRA, which trains only small added matrices From a few hundred to tens of thousands of example dialogues; one GPU for a few hours
A model for a new language or unusual data (DNA, logs, sensor readings), research, or learning how it works Pretraining from scratch Billions of tokens for a useful model; a GPU cluster for serious sizes

Where to get the architecture

You do not need to invent one. An architecture is code plus a configuration: the number of layers, their width, the number of attention heads, the vocabulary size. Open implementations are free:

  • Hugging Face Transformers contains the code of most open architectures, including Llama, Qwen, Gemma, Mistral and OLMo. Create a configuration and a model from it, and you get that architecture with random weights, ready for pretraining; load an existing model with from_pretrained, and you get trained weights to fine-tune. Thousands of models and data sets are on the Hugging Face Hub.
  • nanochat by Andrej Karpathy is one small, readable code base with every stage: tokenizer, pretraining, fine-tuning and a chat interface. It is the best place to learn how the parts fit together.
  • For large runs on many GPUs: torchtitan from the PyTorch team, NVIDIA’s Megatron-LM, Ai2’s OLMo-core and LitGPT handle splitting the model and data across GPUs, checkpoints and recovery from failures.

Where to put the data

The format depends on the stage:

  • For pretraining, plain text. The simplest form is a folder of .txt files; at scale, one document per line in JSONL files with a text field, or Parquet files. The training code trains a tokenizer on this text, converts everything into token IDs, often saving them in binary shards, and then feeds the model random windows of, say, 2,048 tokens.
  • For fine-tuning, example conversations in JSONL, one per line, in the standard chat format that libraries such as Hugging Face TRL expect:
{"messages": [{"role": "user", "content": "How long is the warranty on the X200 router?"}, {"role": "assistant", "content": "The X200 has a two-year warranty. Keep your receipt and contact support to make a claim."}]}

Quality matters more than quantity: a thousand correct, consistent examples teach more than a hundred thousand noisy ones. Keep a few percent of the examples aside to check the model on data it has not seen.

Hands-on: train a tiny language model from scratch

The script below goes through the whole pretraining pipeline on your own text: it reads every .txt file in a data folder, trains a byte-level BPE tokenizer on it, creates a small Llama-style transformer with random weights from a Transformers configuration, trains it to predict the next token and saves it in the standard format. We used a 1.1 MB selection of Shakespeare’s plays as data, the tiny Shakespeare file from Karpathy’s char-rnn repository; replace it with your own files. It needs PyTorch, Transformers and Tokenizers (pip install torch transformers tokenizers) and ran in 12 minutes on a two-core CPU:

import glob
import math
import time

import torch
from tokenizers import ByteLevelBPETokenizer
from transformers import LlamaConfig, LlamaForCausalLM, PreTrainedTokenizerFast

torch.manual_seed(0)

# 1. Your data: put any number of plain-text files into the data folder
files = sorted(glob.glob("data/*.txt"))
text = "".join(open(f, encoding="utf-8").read() for f in files)
print(f"{len(files)} file(s), {len(text):,} characters")

# 2. Tokenizer: learn a vocabulary of 4,096 tokens from your own data
bpe = ByteLevelBPETokenizer()
bpe.train(files, vocab_size=4096, min_frequency=2, special_tokens=["<|endoftext|>"])
tok = PreTrainedTokenizerFast(tokenizer_object=bpe, eos_token="<|endoftext|>", pad_token="<|endoftext|>")
ids = torch.tensor(tok(text)["input_ids"])
print(f"{len(ids):,} tokens, {len(text) / len(ids):.1f} characters per token")
split = int(0.95 * len(ids))
train_ids, val_ids = ids[:split], ids[split:]          # last 5% of the text for validation

# 3. Architecture: a ready-made Llama-style transformer, defined by a config and filled with random weights
config = LlamaConfig(vocab_size=len(tok), hidden_size=256, intermediate_size=704, num_hidden_layers=6,
                     num_attention_heads=8, num_key_value_heads=4, max_position_embeddings=256,
                     tie_word_embeddings=True)
model = LlamaForCausalLM(config)
print(f"{model.num_parameters() / 1e6:.1f}M parameters")

def generate(prompt, n=40):
    model.eval()
    torch.manual_seed(1)
    inputs = tok(prompt, return_tensors="pt")
    out = model.generate(**inputs, max_new_tokens=n, do_sample=True, top_k=50, temperature=0.8,
                         pad_token_id=tok.pad_token_id)
    model.train()
    return tok.decode(out[0]).replace(chr(10), " / ")

# 4. Pretraining: predict the next token, batch after batch
block, batch, steps = 128, 32, 600
def get_batch(src):
    starts = torch.randint(len(src) - block, (batch,))
    return torch.stack([src[s:s + block] for s in starts])

@torch.no_grad()
def val_loss():
    model.eval()
    torch.manual_seed(2)
    loss = sum(model(input_ids=(x := get_batch(val_ids)), labels=x).loss.item() for _ in range(20)) / 20
    model.train()
    return loss

opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=0.1)
warmup = 50
sched = torch.optim.lr_scheduler.LambdaLR(opt, lambda s: min(1, (s + 1) / warmup) * 0.5 * (1 + math.cos(math.pi * s / steps)))
print("before training:", generate("ROMEO:"))
start = time.time()
for step in range(steps + 1):
    if step % 100 == 0:
        v = val_loss()
        print(f"step {step:5d}  validation loss {v:.2f}  perplexity {math.exp(v):7.1f}  {time.time() - start:4.0f} s")
    x = get_batch(train_ids)
    loss = model(input_ids=x, labels=x).loss            # the model shifts the labels by one token itself
    loss.backward()
    torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
    opt.step(); sched.step(); opt.zero_grad()
print("after training: ", generate("ROMEO:"))

# 5. Save in the standard format: reload later with from_pretrained or a Hugging Face pipeline
model.save_pretrained("my-model")
tok.save_pretrained("my-model")
1 file(s), 1,115,394 characters
344,104 tokens, 3.2 characters per token
5.5M parameters
before training: ROMEO: layaudio author wisWhilejoyMOND laboururse treadaudio open steed griMOND Clarence illowersakengrace ThisThTh ras angerWhileTh Clarence creENIUS Clarence ill steed sighs Clarence Clarenceixt loath churchhat
step     0  validation loss 8.38  perplexity  4369.7     7 s
step   100  validation loss 5.30  perplexity   201.3   130 s
step   200  validation loss 4.90  perplexity   133.9   248 s
step   300  validation loss 4.75  perplexity   115.9   365 s
step   400  validation loss 4.67  perplexity   107.0   477 s
step   500  validation loss 4.66  perplexity   105.3   588 s
step   600  validation loss 4.66  perplexity   106.0   700 s
after training:  ROMEO: / I am a noble lord. /  / PETRUCHIO: / I wish thee as it is; but you will tell you what, / As she makes a word to this night of her. /  /

Before training, the model strings random tokens together. After 600 steps the validation loss fell from 8.38 to 4.66; a perplexity of about 106 means the model is, on average, as unsure as if it were choosing among 106 equally likely tokens, against more than 4,000 at the start. It now writes text with the shape of a play: character names, line breaks, archaic words, mostly grammatical lines that do not add up to a story. In a first run with 1,500 steps, the validation loss stopped improving around step 500 and then started to rise: the model began to memorise its 344,000 training tokens, a sign that it needs more data rather than more training. All this is expected from 5.5 million parameters; GPT-2, the smallest model that wrote convincing paragraphs, had 124 million parameters and was trained on about 40 GB of text. The saved model loads like any model from the Hugging Face Hub:

from transformers import pipeline, set_seed

generator = pipeline("text-generation", model="my-model")
set_seed(3)
print(generator("JULIET:", max_new_tokens=40, do_sample=True, top_k=50, temperature=0.8)[0]["generated_text"])
JULIET:
For, then, shall we tell thee to-morrow!
Doy, good my name, my brother, and come to
To give me cray as you will to see it;

From a toy to a real model

  • More data. As a rule of thumb, plan for at least 20 tokens per parameter: about 20 billion tokens for a 1-billion-parameter model. Open data sets such as FineWeb and Dolma provide trillions of tokens of cleaned web text to mix with your own.
  • A GPU. The same script runs on a GPU after moving the model and batches to "cuda"; add bf16 mixed precision and torch.compile for speed and switch to one of the multi-GPU frameworks above when one GPU is not enough. Our guide to cloud GPU pricing helps estimate the budget.
  • Then fine-tune. A pretrained model only continues text. To make it answer questions, train it further on dialogues in the JSONL format above, with TRL’s SFTTrainer or with tools such as Unsloth, Axolotl and LLaMA-Factory.
  • Measure. Track validation loss, test the model on tasks you care about, and compare it with an open model of the same size fine-tuned on the same data. Very often the fine-tuned open model wins, and that is a sign to stop pretraining and fine-tune instead.

Generative AI and AI agents

Generative AI is AI that creates new content rather than just classifying or predicting. Large language models (LLMs) such as ChatGPT, Claude and Gemini write and edit text and code and answer questions; diffusion models generate images and video; other models create music, speech and 3D objects. They work by learning the statistical structure of huge amounts of data and then producing new samples with the same structure, guided by a prompt.

AI agents go one step further: instead of answering a single question, an agent receives a goal, plans the steps, uses tools such as a web browser, code interpreter or company software, checks the results and continues until the task is done. Coding agents are the most mature example; see our beginner guides to Claude Code and OpenAI Codex. Agents can save a great deal of time, but they also make mistakes on their own, so most deployments keep a human in the loop for important actions.

Examples of AI in everyday life

  • Assistants and chatbots: answering questions, drafting emails, summarising documents, tutoring.
  • Search and recommendations: ranking web results, suggesting videos, songs and products.
  • Images and video: face unlock, photo search, smartphone camera processing, AI photo editing and video generation.
  • Language: machine translation, speech recognition, real-time captions.
  • Transport: navigation apps, driver assistance and robotaxis, which already carry paying passengers in several cities.
  • Health and science: reading medical scans, predicting protein structures with AlphaFold, accelerating drug and materials discovery.
  • Business: fraud detection, demand forecasting, customer support, writing and reviewing software.

The scale of adoption is captured in the Stanford AI Index 2026: 88% of organisations report using AI, and generative AI reached 53% of the population within three years. Investment is concentrated: US private AI investment reached $285.9 billion in 2025, about 23 times China’s $12.4 billion, though the report notes that Chinese state funding is not captured in these figures. US labs released 50 notable models and Chinese labs 30, and the performance gap between the best American and Chinese models has narrowed to 2.7%. To run AI yourself, the key hardware is the graphics card; see our guide to the best GPU for AI.

Risks and regulation

The same capabilities that make AI useful create real risks:

  • Errors and hallucinations. Language models can state false information confidently. Important outputs need checking.
  • Bias. Models trained on historical data can reproduce discrimination in hiring, lending or policing.
  • Misuse. Deepfakes, fraud, disinformation and cyberattacks become cheaper. Documented AI incidents rose from 233 in 2024 to 362 in 2025, according to the Stanford AI Index.
  • Jobs. AI automates parts of many jobs. The Stanford report notes that employment of software developers aged 22 to 25 has fallen by almost 20% since 2022, though economists still debate how much of this is caused by AI.
  • Privacy and concentration of power. Training data, compute and the best models are concentrated in a few large companies.
  • Energy and water. AI data centres need enormous amounts of electricity; their global capacity reached 29.6 GW.

Governments are responding. The EU AI Act, the first comprehensive AI law, bans certain practices such as social scoring since February 2025 and has applied rules for general-purpose AI models since August 2025. Its transparency obligations apply from August 2026. The obligations for high-risk systems were postponed by the Digital Omnibus, in force since July 2026, to December 2027 for uses such as hiring and credit scoring, and to August 2028 for AI built into regulated products. Worldwide, 47 countries have active AI legislation.

How close are we to AGI?

Scene from the film Ex Machina showing the humanoid robot Ava, a fictional example of artificial general intelligence
Ava from Ex Machina: the fictional image of AGI is a humanlike robot, the real one is software

Opinions range widely. Leaders of several AI labs say that systems matching humans at most cognitive work could arrive within a few years; many researchers think current methods will hit limits and that fundamentally new ideas are needed, which was McCarthy’s view too. What is clear is how fast the frontier moves: according to the Stanford AI Index 2026, the best scores on the SWE-bench Verified coding benchmark rose from about 60% to near 100% in a single year. At the same time, today’s models still make mistakes no human expert would, struggle to learn continuously from experience, and have limited ability to act reliably over long periods in the physical world.

The useful question for most people is not when AGI arrives, but which tasks AI can already do reliably today, and how to use it safely where it helps.

FAQ

What does AI stand for?

AI stands for artificial intelligence: the field of computer science that builds machines able to perform tasks that normally require human intelligence.

Who invented artificial intelligence?

AI has many founders. Alan Turing described thinking machines in 1947 and 1950. John McCarthy coined the term “artificial intelligence” in 1955 and, with Marvin Minsky, Nathaniel Rochester and Claude Shannon, organised the 1956 Dartmouth workshop where the field was born.

When was AI invented?

The field is usually dated to 1956, the year of the Dartmouth workshop. The first AI programs, such as a checkers player and a theorem prover, appeared in the 1950s.

Is ChatGPT artificial intelligence?

Yes. ChatGPT is a generative AI application built on large language models, a type of deep learning. Like all current AI, it is narrow AI: very capable at language tasks, but not a general intelligence.

What is the difference between AI and machine learning?

AI is the goal of making machines act intelligently by any method. Machine learning is a method: the machine learns from data instead of following hand-written rules. Almost all modern AI is built with machine learning.

What are the main types of AI?

By capability: narrow AI (all AI today), artificial general intelligence and superintelligence (both hypothetical). By functionality: reactive machines, limited memory, theory of mind and self-aware AI, of which only the first two exist.

Can AI think?

It depends on what you mean by thinking. Modern AI can solve problems, reason through steps and hold conversations that judges cannot tell from human ones. Whether that amounts to understanding or consciousness is an open philosophical question; most researchers say current systems are not conscious.


bnr2mob
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted