Llama 3, when fine-tuned for instruction following, actually learns to predict the next token in a way that aligns with the style and content of your custom instructions, rather than magically understanding them.

Let’s see it in action. Imagine we want Llama 3 to act as a helpful assistant that summarizes articles, but only if they’re about technology.

First, we need our custom data. This is a dataset of prompt-response pairs, formatted in a specific way that the model understands. A common format uses special tokens to delineate user input, assistant output, and the overall conversation.

[
  {
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant that summarizes technology articles. If the article is not about technology, politely decline."
      },
      {
        "role": "user",
        "content": "Summarize this article: The latest advancements in AI are revolutionizing healthcare, with new algorithms improving diagnostic accuracy and drug discovery. Machine learning models are being developed to predict patient outcomes and personalize treatment plans. This is a significant leap forward in medical technology."
      },
      {
        "role": "assistant",
        "content": "This article discusses how AI is transforming healthcare through improved diagnostics, drug discovery, patient outcome prediction, and personalized treatment plans, marking a major advancement in medical technology."
      }
    ]
  },
  {
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant that summarizes technology articles. If the article is not about technology, politely decline."
      },
      {
        "role": "user",
        "content": "Summarize this article: The new bakery downtown offers artisanal sourdough bread and French pastries. Their croissants are flaky and delicious, and they also serve locally roasted coffee."
      },
      {
        "role": "assistant",
        "content": "I can only summarize articles about technology. This article appears to be about a bakery."
      }
    ]
  }
]

The system message sets the persona and the core instruction. The user messages are the inputs, and the assistant messages are the desired outputs. The fine-tuning process takes a pre-trained Llama 3 model (which already has a vast understanding of language) and trains it further on this specific dataset. The model adjusts its internal weights to make generating responses similar to the assistant examples more probable when given prompts like the user examples, especially when the system instruction is present.

The core problem this solves is that general-purpose LLMs are trained on a massive, diverse corpus of text. While they can follow simple instructions, they often lack the specificity, tone, or domain knowledge required for particular tasks. Fine-tuning bridges this gap by exposing the model to curated examples that demonstrate the desired behavior.

Internally, the process involves feeding these prompt-response pairs to the model. For each example, the model tries to predict the sequence of tokens that form the assistant’s response, given the user’s prompt and the system instruction. The "loss" function measures how far off the model’s predictions are from the actual assistant response. Backpropagation then updates the model’s weights to reduce this loss. Over many examples, the model learns to generalize this behavior.

The exact levers you control are primarily the dataset itself and the training parameters. The dataset dictates what the model learns: the style, the format, the knowledge, and the specific instruction-following behavior. Parameters like learning_rate, num_train_epochs, and per_device_train_batch_size control how it learns. A higher learning_rate can lead to faster convergence but risks overshooting the optimal weights. More epochs mean more passes through the data, potentially leading to better learning but also overfitting. The batch_size affects gradient estimation stability and memory usage.

A common misconception is that fine-tuning imparts new "knowledge" in the way a human gains knowledge. Instead, it’s about learning to retrieve and present information or generate text in a specific style that is already implicitly present in the vast pre-training data, but now guided by your specific examples. The model isn’t understanding the concept of "technology article" in a human sense; it’s learning that certain patterns of tokens in the user input, when combined with the system instruction, should lead to token sequences that resemble your assistant responses, including the refusal to summarize non-tech articles.

Once you’ve fine-tuned, the next hurdle is often evaluating the model’s performance comprehensively, as simple accuracy metrics can be misleading for generative tasks.

Want structured learning?

Take the full Fine-tuning course →