Usage
Basic Agent
Run an agent on a HuggingFace model in sync, streaming, and async modes.
The async examples share one event loop because the model caches its inference client. See the Hugging Face setup for token permissions and model-provider availability.
Code
import asyncio
from agno.agent import Agent
from agno.models.huggingface import HuggingFace
model = HuggingFace(
id="mistralai/Mistral-7B-Instruct-v0.2", max_tokens=4096, temperature=0
)
agent = Agent(model=model)
async def async_examples():
# Reuse and close the cached async client in one event loop.
try:
await agent.aprint_response(
"What is meaning of life and then recommend 5 best books to read about it"
)
await agent.aprint_response(
"What is meaning of life and then recommend 5 best books to read about it",
stream=True,
)
finally:
await model.get_async_client().close()
if __name__ == "__main__":
# --- Sync ---
agent.print_response(
"What is meaning of life and then recommend 5 best books to read about it"
)
# --- Sync + Streaming ---
agent.print_response(
"What is meaning of life and then recommend 5 best books to read about it",
stream=True,
)
# --- Async and Async + Streaming ---
asyncio.run(async_examples())
model.get_client().close()Usage
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateSet your API key
export HF_TOKEN=xxxInstall dependencies
uv pip install -U huggingface_hub agnoRun Agent
Save the code above as huggingface_basic.py, then run:
python huggingface_basic.py