翻篇收录说明
翻篇网页快照

Async Applications with Agents · Hugging Face

https://huggingface.co/docs/smolagents/examples/async_agent

最近抓取:2026-12-01 11:09:42(北京时间)

最近内容变化:2026-12-01 11:09:42(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

We’re on a journey to advance and democratize artificial intelligence through open source and open science.

正文摘录

smolagents documentation Async Applications with Agents smolagents 🏡 View all docsAWS Trainium & InferentiaAccelerateBitsandbytesCLIChat UIDataset viewerDatasetsDeploying on AWSDiffusersGoogle CloudGoogle TPUsGradioHubHub Python LibraryHuggingface.jsInference Endpoints (dedicated)Inference ProvidersKernelsLeRobotLeaderboardsLightevalMicrosoft AzureOpenEnvOptimumPEFTReachy MiniSafetensorsSentence TransformersTRLTasksText Embeddings InferenceTokenizersTrackioTransformersTransformers.jsXetsmolagentstimm Search documentation mainv1.26.0v1.25.0v1.24.0v1.23.0v1.22.0v1.21.2v1.20.0v1.19.0v1.18.0v1.17.0v1.16.1v1.15.0v1.14.0v1.13.0v1.12.0v1.11.0v1.10.0v1.9.2v1.8.1v1.7.0v1.6.0v1.5.1v1.4.1v1.3.0v1.2.2v1.1.0v0.1.3 ENHIKOZH Join the Hugging Face community and get access to the augmented documentation experience Collaborate on models, datasets and Spaces Faster examples with accelerated inference Switch between documentation themes Sign Up to get started Copy page Async Applications with Agents This guide demonstrates how to integrate a synchronous agent from the smolagents library into an asynchronous Python web application using Starlette. The example is designed to help users new to async Python and agent integration understand best practices for combining synchronous agent logic with async web servers. Overview Starlette: A lightweight ASGI framework for building asynchronous web applications in Python. anyio.to_thread.run_sync: Utility to run blocking (synchronous) code in a background thread, preventing it from blocking the async event loop. CodeAgent: An agent from the smolagents library capable of programmatically solving tasks. Why Use a Background Thread? CodeAgent.run() executes Python code synchronously. If called directly in an async endpoint, it would block Starlette’s event loop, reducing performance and scalability. By offloading this operation to a background thread with anyio.to_thread.run_sync, you keep the app responsive and efficient, even under high concurrency. Example Workflow The Starlette app exposes a /run-agent endpoint that accepts a JSON payload with a task string. When a request is received, the agent is run in a background thread using anyio.to_thread.run_sync. The result is returned as a JSON response. Building a Starlette App with a CodeAgent 1. Install Dependencies Copied pip install smolagents starlette anyio uvicorn 2. Application Code ( main.py ) Copied import anyio.to_thread from starlette.applications import Starlette from starlette.requests import Request from starlette.responses import JSONResponse from starlette.routing import Route from smolagents import CodeAgent, InferenceClientModel agent = CodeAgent( model=InferenceClientModel(model_id="Qwen/Qwen3-Next-80B-A3B-Thinking"), tools=[], ) async def run_agent(request: Request): data = await request.json() task = data.get("task", "") # Run the agent synchronously in a background thread result = await anyio.to_thread.run_sync(agent.run, task) return JSONResponse({"result": result}) app = Starlette(routes=[ Route("/run-agent", run_agent, methods=["POST"]), ]) 3. Run the App Copied uvicorn async_agent.main:app --reload 4. Test the Endpoint Copied curl -X POST http://localhost:8000/run-agent -H 'Content-Type: application/json' -d '{"task": "What is 2+2?"}' Expected Response: Copied {"result": "4"} Further Reading Starlette Documentation anyio Documentation For the full code, see examples/async_agent. Update on GitHub ←Human-in-the-Loop: Customize agent plan interactively Agents→