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 memoryTransformers basics
Capabilities in a multi-agent system
Transformers models typically serve as tools or lightweight agents. The following table lists common uses.
| Capability | Model type | Role in the solution |
|---|---|---|
| Intent routing | Text or zero-shot classifier | Hub decides which specialist agent handles a request |
| Reranking | Cross-encoder | Re-scores retrieved passages for precision |
| Embeddings | Sentence embedding model | Local semantic search or similarity routing |
| Entity and PII detection | Token classifier (NER) | Redaction before memory storage or logging |
| Guard checks | Fine-tuned classifier | Domain-specific custom guardrail |
| Narrow generation | Small fine-tuned language model | High-volume, well-defined text tasks |
Transformers capabilities in an agent system
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.
Adopting an open model
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.
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.