Arian Soleimanzadeh
  • Home
  • Blog
  • Podcasts
  • Videos
  • Library
  • Contact
العربيةArabic
DeutschGerman
EnglishEnglish
فارسیPersian
한국어Korean
中文Chinese
Panel•Quick Contact

Languages

Choose your interface locale

ar

العربية

Arabic

de

Deutsch

German

en

English

English

fa

فارسی

Persian

ko

한국어

Korean

zh

中文

Chinese

Get Appointment

Drop a quick message — I’ll reply as soon as possible.

LinkedInFast response
Home/Articles/LLM Fundamentals: Transformer, Prompting, Context Windows, Hallucination and Tool Use
Artificial IntelligenceLLMGenerative AIArticle

LLM Fundamentals: Transformer, Prompting, Context Windows, Hallucination and Tool Use

A practical introduction to Large Language Models, explaining how Transformers work, how prompts control behavior, what context windows mean, why hallucinations occur, and how LLMs safely interact with real APIs and software tools.

September 11, 202618 min read18 Views
#LLM#Transformer#Prompt Engineering#Context Window#Hallucination#Tool Use#Function Calling#RAG#AI Agents

Arian Soleimanzadeh

Software Engineer & Researcher

Conceptual illustration of an LLM architecture showing Transformer, prompting, context, hallucination control and tool use

Arian Soleimanzadeh

AI · Code · Product

Research + Engineering
On this page
1. What is an LLM?2. TokensEmbeddingsQuery, Key and ValueMulti-Head AttentionPositionFeed-Forward LayersZero-Shot PromptingFew-Shot PromptingMore Context Is Not Always BetterGroundingRAGTool UseExplicit InstructionsStructured OutputValidationHuman ApprovalStep 1 — Understand IntentStep 2 — Retrieve DataStep 3 — AnalyzeStep 4 — DecideStep 5 — Propose ActionStep 6 — AuthorizationStep 7 — ConfirmationStep 8 — Execute

Large Language Models, or LLMs, power many modern AI systems, including conversational assistants, coding tools, enterprise search systems and autonomous agents.

You do not need to master every mathematical detail before using an LLM professionally. You do, however, need to understand what the model actually does, what it does not know, how information reaches it, and how it should be connected to real software systems.

This article focuses on five foundations:

  1. Transformer architecture
  2. Prompting
  3. Context windows
  4. Hallucination
  5. Tool use

1. What is an LLM?

At its core, an LLM is trained to predict the next token given the tokens that came before it.

Given:

The capital of France is

it will assign a high probability to a continuation such as Paris.

When this objective is trained across enormous datasets using very large neural networks, surprisingly powerful abilities emerge: summarization, translation, programming, question answering, reasoning and planning.

The critical point is that an LLM is not inherently a database of verified facts. It generates likely continuations. This explains both its usefulness and many of its failure modes.


2. Tokens

Models do not normally process text as complete human words. Text is divided into smaller units called tokens.

A token might represent a word, part of a word, punctuation or another text fragment.

Token count matters because it affects:

  • API cost
  • latency
  • maximum input size
  • maximum output size
  • context-window usage

A 128K context window therefore means approximately 128K tokens, not 128K words.


3. Transformer Architecture

The Transformer is the core architecture behind most modern LLMs.

Its most important mechanism is attention, which lets the model determine how different parts of a sequence relate to each other.

Consider:

Sara gave Mina the book because she needed it.

To understand she, the model needs to determine which earlier words are relevant. Attention helps the model calculate those relationships.

The standard attention equation is:

Attention(Q,K,V) = softmax(QKᵀ / √dₖ)V

You do not need to memorize this equation to build applications. The practical interpretation is more important:

Attention allows the model to decide which previous tokens matter most while processing the current token.


Embeddings

Before a Transformer can process text, tokens are converted into numerical vectors called embeddings.

An embedding represents information about a token in a multidimensional numerical space.

Tokens with related meanings may develop representations that capture similar semantic properties.


Query, Key and Value

Attention internally creates three representations:

  • Query (Q)
  • Key (K)
  • Value (V)

A useful mental model is:

  • Query: what information am I looking for?
  • Key: what information is available here?
  • Value: what information should be transferred if it is relevant?

Multi-Head Attention

Transformers usually run several attention mechanisms in parallel.

This is called Multi-Head Attention.

Different heads can learn different relationships, such as syntax, semantic similarity, references between pronouns and nouns, or dependencies between distant parts of a document.


Position

Order matters in language.

Dog bites man

and

Man bites dog

contain similar words but have very different meanings.

Transformers therefore need information about token positions. Different architectures use mechanisms such as positional encoding or rotary position embeddings.


Feed-Forward Layers

After attention, token representations pass through feed-forward neural networks.

A simplified Transformer pipeline is:

Tokens → Embeddings → Attention → Feed Forward → Repeat across layers → Prediction

Large models may contain dozens or more Transformer layers.


4. How Text Generation Works

Suppose the prompt is:

Explain machine learning in simple words.

The model processes the prompt and calculates probabilities for possible next tokens.

One token is selected, added to the sequence, and the model performs another prediction.

Generation therefore works approximately like this:

Prompt → token → token → token → token ...

This continues until the response ends or an output limit is reached.


5. Temperature

Temperature controls how deterministic or varied token selection becomes.

Low temperature is generally useful for:

  • extraction
  • classification
  • structured JSON
  • tool selection
  • deterministic business workflows

Higher temperature is more appropriate for:

  • brainstorming
  • creative writing
  • generating alternatives

Higher temperature does not make a model more intelligent. It primarily makes generation less deterministic.


6. Prompting

A prompt is not merely a question. In a production application, it acts as an interface between your software and the model.

A good prompt should clearly define:

  • the task
  • available information
  • constraints
  • expected behavior
  • required output format

Weak prompt:

Analyze this customer.

Better prompt:

Analyze the customer information below. Classify churn risk as low, medium or high. Give the three most important reasons. Do not invent missing information.


Zero-Shot Prompting

The model receives an instruction without examples.

Example:

Classify this message as complaint, question or purchase request.


Few-Shot Prompting

The prompt includes examples of the desired behavior.

For example:

I want a refund → complaint

How much does it cost? → question

I want to order ten units → purchase_request

Few-shot prompting is especially useful when your categories or output conventions are domain-specific.


7. System and User Messages

LLM APIs commonly support different message roles.

A system instruction might say:

You are a CRM assistant. Never modify customer records without proper authorization.

The user may then say:

Mark Acme's opportunity as Won.

Keeping system policies separate from user requests is important in agent systems because users should not control the application's core rules.


8. Structured Output

Production systems often need predictable data rather than natural-language prose.

For example:

JSON
12345
{
  "risk": "high",
  "confidence": 0.81,
  "reason": "Repeated service failures"
}

Structured output makes model results easier for software to consume.

However:

Never assume that output is valid merely because the model produced JSON. Validate it in your application.


9. Context Window

The context window is the amount of information the model can consider in a single inference.

It may contain:

  • system instructions
  • conversation history
  • current user request
  • retrieved documents
  • tool results
  • examples
  • generated output

You can think of it as the model's temporary working memory.

It is not permanent memory.

When conversations or documents become too large, older or less important information may need to be removed, summarized or retrieved only when required.


More Context Is Not Always Better

Sending everything to the model is usually poor architecture.

An excessively large context can cause:

  • higher cost
  • higher latency
  • irrelevant information
  • contradictory information
  • important instructions becoming harder to identify

A better rule is:

Provide enough relevant context, not every piece of available context.


10. RAG and Context

Imagine an organization with 100,000 internal documents.

Putting every document into a prompt is impossible and unnecessary.

With Retrieval-Augmented Generation (RAG), the system first retrieves relevant information and only places the most useful passages into the model's context.

Typical flow:

Question → Retrieval → Relevant Documents → LLM → Answer

RAG is widely used for enterprise knowledge assistants and document-based applications.


11. Hallucination

A hallucination occurs when a model produces information that appears plausible but is unsupported or false.

It may invent:

  • citations
  • product features
  • API methods
  • customer information
  • statistics
  • historical facts

This happens because the model's basic objective is generating a plausible continuation, not guaranteeing factual correctness.


12. Reducing Hallucinations

Hallucination cannot currently be eliminated completely, but it can be reduced substantially.

Useful techniques include:

Grounding

Provide reliable information in the context.

RAG

Retrieve facts from trusted sources.

Tool Use

Read live values such as prices, account information and order status from real APIs.

Explicit Instructions

Tell the model not to guess when information is unavailable.

Structured Output

Constrain what the model needs to return.

Validation

Verify outputs programmatically.

Human Approval

Require approval before consequential actions.


13. Tool Use

An LLM by itself usually cannot perform real operations in your software environment.

It cannot safely assume current information about:

  • database records
  • order status
  • inventory
  • account permissions
  • live prices

Nor should it directly modify those systems.

Instead, the application exposes controlled tools such as:

get_customer(customer_id)

get_order_status(order_id)

create_lead(data)

send_email(to, subject, body)

The model determines when a tool may be useful, while your application controls whether and how the tool is actually executed.


14. Function Calling Flow

Suppose a user asks:

Check order 5421.

A robust flow is:

  1. User submits the request.
  2. LLM identifies the need for order information.
  3. LLM proposes get_order_status(5421).
  4. Backend validates the arguments.
  5. Backend calls the real service.
  6. Tool result is returned to the model.
  7. LLM explains the result to the user.

Architecture:

User → LLM → Tool Request → Backend → API/Database → Tool Result → LLM → User

Tool use is one of the foundations of modern AI agents.


15. Never Trust a Tool Call Automatically

Suppose the model proposes:

delete_customer(218)

Your backend must not execute the action simply because the model requested it.

Before execution, check:

  • authentication
  • authorization
  • input schema
  • business rules
  • risk level
  • user confirmation when necessary

A useful principle is:

The LLM may propose an action. Your application remains the authority that decides whether the action is permitted.


16. Practical Example: CRM Agent

Consider this request:

Check the latest information for Acme and mark the opportunity as Won if it is ready.

A production architecture could perform the following steps.

Step 1 — Understand Intent

The LLM identifies that information must first be inspected and that a write operation may follow.

Step 2 — Retrieve Data

The application executes:

get_account('Acme')

Step 3 — Analyze

The LLM analyzes real CRM data rather than inventing customer state.

Step 4 — Decide

If essential information is missing, the agent should ask for clarification instead of guessing.

Step 5 — Propose Action

If appropriate, the model proposes the opportunity update tool.

Step 6 — Authorization

The backend verifies that the current user is allowed to update the opportunity.

Step 7 — Confirmation

For important changes, the system can request explicit confirmation.

Step 8 — Execute

Only then does the backend update the CRM.

This distinction separates an impressive demo from a dependable production agent.


17. A Practical LLM Application Architecture

A useful starting architecture is:

Frontend

↓

Backend / Agent Controller

↓

LLM

↓

Tool Router

↓

CRM / Database / Search / External APIs

The backend or agent controller should typically handle:

  • authentication
  • authorization
  • context construction
  • prompt management
  • tool permissions
  • validation
  • logging
  • retries
  • rate limits
  • risk controls

Do not force the LLM to become your entire application architecture.


18. Prompt vs Code

Use prompts for things such as:

  • task description
  • response style
  • reasoning guidance
  • behavioral policies
  • output structure

Use deterministic code for:

  • authorization
  • authentication
  • financial limits
  • schema validation
  • security controls
  • critical business rules
  • actual execution

If a rule is critical for security, do not protect it only with a prompt.


19. Debugging LLM Applications

When an LLM application fails, changing the prompt is not always the answer.

Failures can originate from:

  1. poor instructions
  2. irrelevant context
  3. weak retrieval
  4. ambiguous tool descriptions
  5. incorrect tool results
  6. missing validation
  7. unsuitable model choice
  8. inappropriate temperature
  9. conflicting information

Production systems should therefore trace flows such as:

User Request → Retrieved Context → Model Decision → Tool Call → Tool Result → Final Response

Observability is essential for debugging agents.


20. Project Checklist

Before shipping an LLM feature, ask:

  • Is the model's responsibility clearly defined?
  • Is the system prompt explicit?
  • Is structured output used where appropriate?
  • Does context contain only relevant information?
  • Are facts retrieved from trustworthy systems?
  • Is hallucination risk managed?
  • Are tool arguments validated?
  • Is authorization checked outside the LLM?
  • Do sensitive actions require approval?
  • Are tool calls logged?
  • Are timeouts and failures handled?
  • Are high-impact outputs validated before use?

Mental Model

The entire topic can be summarized as follows:

An LLM is fundamentally a next-token prediction engine.

The Transformer helps it model relationships between tokens.

Prompts define the current task and behavioral instructions.

The context window determines what information the model can currently see.

Hallucination means plausible language does not necessarily equal truth.

Tool use connects language-model reasoning with real software and real data.

Most importantly:

A capable LLM does not automatically create a trustworthy AI system. Trustworthy systems combine models with relevant context, controlled tools, validation, permissions, observability and sound software architecture.

Once these foundations are clear, you are ready to move into AI agents, ReAct, planning, memory, reflection, RAG, orchestration and adaptive autonomy.

On this page
1. What is an LLM?2. TokensEmbeddingsQuery, Key and ValueMulti-Head AttentionPositionFeed-Forward LayersZero-Shot PromptingFew-Shot PromptingMore Context Is Not Always BetterGroundingRAGTool UseExplicit InstructionsStructured OutputValidationHuman ApprovalStep 1 — Understand IntentStep 2 — Retrieve DataStep 3 — AnalyzeStep 4 — DecideStep 5 — Propose ActionStep 6 — AuthorizationStep 7 — ConfirmationStep 8 — Execute

Article details

Publication metadata, reading time and live view information.

Published

September 11, 2026

Updated

November 6, 2026

Reading time

18 min read

Views

18

Author

Arian Soleimanzadeh

Previous article

What Is Hermes Agent? A Complete Guide to Building a Private, Local AI Agent

Next article

AI Agents and Agentic AI: Agent Loops, Planning, Memory and Tool Calling

Arian Soleimanzadeh

Personal portfolio focused on modern web engineering, UI systems, and practical AI products — clean code, clean design.

Quick Links

  • About
  • Blog
  • Library
  • Contact

Contact

  • soleimanzadeh.a.work@gmail.com
  • soleimanzadeh.uni@gmail.com

Availability: Weekdays

Typically replies within 24h.

Newsletter

Get updates on posts, projects, and new releases.

© 2026 ariansoleimanzadeh.dev — All rights reserved.

LinkedIn