Phi-4 Microsoft SLM 9 min read

Phi-4 Guide 2025: Microsoft's Best Small Language Model

Microsoft Phi-4 is the benchmark-breaking small language model family that outperforms models 5x its size. At 14B parameters, Phi-4 beats Llama 3 70B on MMLU, GSM8K, and HumanEval — making it the top choice for on-device deployment, edge inference, and cost-efficient cloud APIs.

What Is Phi-4?

Phi-4 is Microsoft Research's fourth-generation small language model family, released in late 2024. Unlike most SLMs that are distilled from larger models, Phi-4 was trained from scratch on a carefully curated mixture of high-quality synthetic data generated by stronger models plus filtered web content. This data-quality-first approach is the reason Phi-4 achieves GPT-4-class reasoning at a fraction of the parameter count.

The Phi-4 family has three members:

  • Phi-4 (14B) — flagship model, best-in-class for STEM reasoning and coding under 15B parameters.
  • Phi-4-mini (3.8B) — ultra-compact model for edge and on-device inference. Outperforms most 7B models on reasoning benchmarks.
  • Phi-4-multimodal (5.6B) — vision + audio + text in a single model. Designed for multimodal applications on constrained hardware.

All three models use a 128K context window and are licensed under MIT, making them free for commercial use.

Model Variants and Availability

Phi-4 models are available across multiple platforms. Choose the access method that fits your workflow:

  • Hugging Face Hubmicrosoft/phi-4, microsoft/phi-4-mini, microsoft/phi-4-multimodal-instruct. Full weights available in BF16 and GGUF format.
  • Azure AI Foundry — Serverless pay-per-token API endpoints, no GPU provisioning required. Supports both Phi-4 and Phi-4-mini.
  • Azure OpenAI Service — Phi-4 accessible via familiar OpenAI SDK with Azure authentication in supported regions.
  • Ollamaollama run phi4 downloads and runs a quantized version locally in one command.
  • ONNX Runtime — Microsoft ships official ONNX exports for on-device deployment on Windows and mobile hardware.

Run Locally with Ollama

The fastest way to run Phi-4 on your own machine. Ollama handles quantization and model management automatically. Phi-4 at Q4_K_M quantization needs about 8 GB of RAM or VRAM:

Pull and run Phi-4

# Run Phi-4 14B (recommended for most use cases)
ollama run phi4

# Run Phi-4-mini 3.8B (fast, low-memory option)
ollama run phi4-mini

API call via Ollama REST endpoint

curl http://localhost:11434/api/chat -d '{
  "model": "phi4",
  "messages": [
    {
      "role": "user",
      "content": "Solve step by step: if x^2 - 5x + 6 = 0, find x."
    }
  ],
  "stream": false
}'

Ollama also exposes an OpenAI-compatible endpoint at http://localhost:11434/v1 — any OpenAI SDK client works with base_url="http://localhost:11434/v1".

Python with Transformers (Hugging Face)

Load microsoft/phi-4 directly from the Hugging Face Hub using the transformers library. The model uses a standard chat template compatible with the apply_chat_template API:

phi4_chat.py

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "microsoft/phi-4"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto",
)

messages = [
    {"role": "system", "content": "You are a helpful AI assistant."},
    {"role": "user", "content": "Write a Python function to check if a number is prime."},
]

# Apply the model's chat template
inputs = tokenizer.apply_chat_template(
    messages,
    tokenize=True,
    add_generation_prompt=True,
    return_tensors="pt",
).to(model.device)

output_ids = model.generate(
    inputs,
    max_new_tokens=512,
    temperature=0.7,
    do_sample=True,
)

# Decode only the new tokens
response = tokenizer.decode(
    output_ids[0][inputs.shape[1]:],
    skip_special_tokens=True,
)
print(response)

Phi-4 14B requires about 28 GB VRAM in BF16. Use load_in_4bit=True with BitsAndBytesConfig to fit it in 10 GB VRAM on a consumer GPU.

4-bit quantization for consumer GPUs

from transformers import BitsAndBytesConfig

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
)

model = AutoModelForCausalLM.from_pretrained(
    model_id,
    quantization_config=bnb_config,
    device_map="auto",
)

Azure AI Foundry Serverless Endpoint

Azure AI Foundry offers Phi-4 as a managed serverless API — no GPU provisioning, pay per token. You can call it with the Azure AI Inference SDK or the standard OpenAI SDK pointed at your Azure endpoint:

Install the Azure AI Inference SDK

pip install azure-ai-inference

azure_phi4.py — Azure AI Inference SDK

from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.core.credentials import AzureKeyCredential
import os

client = ChatCompletionsClient(
    endpoint=os.environ["AZURE_AI_ENDPOINT"],
    credential=AzureKeyCredential(os.environ["AZURE_AI_KEY"]),
)

response = client.complete(
    model="Phi-4",
    messages=[
        SystemMessage(content="You are a helpful AI assistant."),
        UserMessage(content="Explain the Pythagorean theorem with a code example."),
    ],
    max_tokens=512,
    temperature=0.7,
)

print(response.choices[0].message.content)

Alternative: OpenAI SDK with Azure endpoint

from openai import AzureOpenAI
import os

client = AzureOpenAI(
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_key=os.environ["AZURE_OPENAI_KEY"],
    api_version="2024-08-01-preview",
)

response = client.chat.completions.create(
    model="phi-4",  # deployment name in your Azure resource
    messages=[
        {"role": "user", "content": "What is the time complexity of merge sort?"}
    ],
)
print(response.choices[0].message.content)

Phi-4 vs Llama 3.1 8B vs Gemma 2 9B vs Mistral 7B

Phi-4 is consistently the top-performing model under 15B parameters across reasoning, math, and coding benchmarks:

Model MMLU HumanEval GSM8K MATH
Phi-4 14B 84.8% 82.6% 91.2% 80.4%
Llama 3.1 8B 66.7% 72.6% 84.5% 51.9%
Gemma 2 9B 71.3% 71.1% 87.3% 44.7%
Mistral 7B v0.3 64.1% 60.4% 52.2% 28.4%

Phi-4 achieves these scores with a 14B parameter count — roughly equivalent to running two Llama 3.1 8B models in parallel, but with significantly better reasoning quality. The gap is largest on MATH and GSM8K, confirming the synthetic-data training approach's particular strength in mathematical reasoning.

Best Use Cases for Phi-4

Phi-4's strengths make it ideal for specific workloads where quality-per-parameter matters:

  • STEM reasoning and math tutoring — Phi-4's GSM8K and MATH scores rival much larger models. Ideal for educational apps, homework helpers, and scientific calculators with natural language interfaces.
  • Coding assistance — With 82.6% on HumanEval, Phi-4 generates correct, idiomatic code across Python, JavaScript, C++, and SQL. Suitable for IDE copilots and code review bots.
  • On-device deployment — Phi-4-mini at 3.8B can run entirely on a modern smartphone with hardware acceleration (CoreML on iOS, ONNX Runtime on Android and Windows). Microsoft ships official ONNX exports for this purpose.
  • Edge inference — Phi-4 14B runs on a single A10G GPU (24 GB VRAM), making it viable for edge servers in factories, hospitals, or retail — anywhere you need AI without cloud latency.
  • Cost-efficient cloud APIs — Azure AI Foundry Phi-4 serverless pricing is significantly cheaper than GPT-4o while delivering comparable STEM performance. A good default for reasoning-heavy pipelines watching costs.

Phi-4-multimodal: Vision + Audio + Text

Phi-4-multimodal (5.6B parameters) is a unified model that accepts images, audio, and text in the same prompt — no separate encoder models required. It is designed for on-device scenarios where loading multiple models is impractical.

Capabilities include: image description and VQA, audio transcription and classification, document understanding from screenshots, and interleaved image-text reasoning. Here is an image description example using the Hugging Face pipeline:

phi4_multimodal.py — image description

from transformers import AutoModelForCausalLM, AutoProcessor
from PIL import Image
import torch, requests

model_id = "microsoft/phi-4-multimodal-instruct"

processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto",
    trust_remote_code=True,
)

# Load an image from URL
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png"
image = Image.open(requests.get(url, stream=True).raw)

# Build a multimodal prompt
messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": image},
            {"type": "text", "text": "Describe this image in detail."},
        ],
    }
]

inputs = processor(messages, return_tensors="pt").to(model.device)

output = model.generate(**inputs, max_new_tokens=256)
response = processor.decode(output[0], skip_special_tokens=True)
print(response)

Phi-4-multimodal fits in about 12 GB VRAM in BF16. On mobile hardware via ONNX Runtime it runs at interactive speeds (300-500 ms per response on Apple A17 Pro and Snapdragon 8 Gen 3).

Running Phi-4 via Azure AI?

Running Phi-4 via Azure AI? Prismix monitors Azure OpenAI and Azure service status in real time.

Check Azure AI Status →