Lesson 4 of 5 Human Oversight, Caching, Scaling and Middleware

Using Hugging Face Transformers in Multi-Agent Solutions

This lesson explains how open models from Hugging Face, run with the Transformers library, perform classification, extraction, embedding and reranking faster, cheaper or with data kept local, and how they fit into multi-agent solutions.

Learning outcomes

After completing this lesson, you will be able to:

  • Run a pretrained model with the Transformers pipeline() function.
  • Identify the roles Transformers models play in a multi-agent system.
  • Choose hosting and governance for open models.

The Transformers library

Hugging Face Transformers is a Python library that downloads and runs pretrained models from the Hugging Face Hub. The pipeline() function wraps a model for a task with preprocessing and postprocessing; AutoTokenizer and AutoModelFor... classes give lower-level control and are used for fine-tuning.

from transformers import pipeline

router = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
pii = pipeline("token-classification", model=PII_MODEL, aggregation_strategy="simple")

intent = router("My card was charged twice", candidate_labels=["billing", "delivery", "account", "product"])
route_to = intent["labels"][0]                       # fast, local routing decision before any LLM call
entities = pii("Call Dana Whitfield on 020 7946 0958")  # detect personal data before storing memory
True or false

Transformers basics

Exam skills 2.4.7

Check how the Transformers library runs models.

Log in to try this activity Interactive activities are free with an account.

Capabilities in a multi-agent system

Transformers models typically serve as tools or lightweight agents. The following table lists common uses.

CapabilityModel typeRole in the solution
Intent routingText or zero-shot classifierHub decides which specialist agent handles a request
RerankingCross-encoderRe-scores retrieved passages for precision
EmbeddingsSentence embedding modelLocal semantic search or similarity routing
Entity and PII detectionToken classifier (NER)Redaction before memory storage or logging
Guard checksFine-tuned classifierDomain-specific custom guardrail
Narrow generationSmall fine-tuned language modelHigh-volume, well-defined text tasks
Match the pairs

Transformers capabilities in an agent system

Exam skills 2.4.7

Match each capability with the model type that provides it.

Log in to try this activity Interactive activities are free with an account.

Hosting and governance

Transformers models run inside an agent's container (CPU is enough for many small classifiers), on Azure Container Apps with serverless GPUs, on AKS, on Azure Machine Learning online endpoints, or are deployed from the Hugging Face collection in Foundry Models as managed endpoints. Choosing a model involves its licence (some prohibit commercial use), its model card (training data, intended use, limitations), and evaluation on the solution's own data. Models are pinned to a specific revision so that an upstream update cannot change behaviour unexpectedly.

Select all that apply

Adopting an open model

Exam skills 2.4.7

Pick the checks a team should make before using a Hugging Face model in production.

Log in to try this activity Interactive activities are free with an account.
Worked example

A costly router

A hub agent uses a large hosted model only to decide which of six specialists should handle each of 3 million monthly requests. Routing adds 900 ms of latency and a third of the solution's model cost. Labelled routing data from six months of traffic is available.

A small text classifier fine-tuned with Transformers on the labelled routing data, hosted in the hub's container, can route in milliseconds at negligible cost; low-confidence predictions can fall back to the hosted model. The classifier is evaluated against the current router before replacing it.

Check yourself

Which tasks suit small local Transformers models in a multi-agent solution?

Show answer

Routing, reranking, PII detection and guard checks, which need speed and low cost more than broad reasoning.

Exam Tips

Remember for the exam

  • pipeline() runs pretrained models for classification, NER, embeddings and more.
  • Small local models suit routing, reranking, PII detection and guard checks.
  • Check licence and model card, evaluate on your data and pin the model revision.