Skip to content

Commit 2f132b8

Browse files
committed
docs: add AI agents guide
1 parent 726620b commit 2f132b8

5 files changed

Lines changed: 227 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Almost any Python project can become an Actor, including projects for:
116116
- **Web scraping and crawling** — The SDK is fully compatible with [Crawlee](https://crawlee.dev/python), which makes Apify a natural place to deploy and scale your crawlers (see the [Crawlee guide](https://docs.apify.com/sdk/python/docs/guides/crawlee)). It also works with other popular scraping libraries, such as [Scrapy](https://docs.apify.com/sdk/python/docs/guides/scrapy), [Scrapling](https://docs.apify.com/sdk/python/docs/guides/scrapling), or [Crawl4AI](https://docs.apify.com/sdk/python/docs/guides/crawl4ai).
117117
- **Browser automation** — Drive a real browser with [Playwright](https://docs.apify.com/sdk/python/docs/guides/playwright) or [Selenium](https://docs.apify.com/sdk/python/docs/guides/selenium), or with higher-level tools such as [Browser Use](https://docs.apify.com/sdk/python/docs/guides/browser-use).
118118
- **Web servers and APIs** — Run a [web server](https://docs.apify.com/sdk/python/docs/guides/running-webserver) inside an Actor to serve HTTP requests, for example to expose your scraper as a live API.
119-
- **AI agents** — Host agents built with your framework of choice. Ready-made Actor templates cover [PydanticAI](https://apify.com/templates/python-pydanticai), [CrewAI](https://apify.com/templates/python-crewai), [LangGraph](https://apify.com/templates/python-langgraph), [LlamaIndex](https://apify.com/templates/python-llamaindex-agent), and [Smolagents](https://apify.com/templates/python-smolagents).
119+
- **AI agents** — Host agents built with your framework of choice (see the [AI agents guide](https://docs.apify.com/sdk/python/docs/guides/ai-agents)). Ready-made Actor templates cover [PydanticAI](https://apify.com/templates/python-pydanticai), [CrewAI](https://apify.com/templates/python-crewai), [LangGraph](https://apify.com/templates/python-langgraph), [LlamaIndex](https://apify.com/templates/python-llamaindex-agent), and [Smolagents](https://apify.com/templates/python-smolagents).
120120
- **MCP servers** — Deploy a Python MCP server as an Actor and make its tools available to any MCP client. See [MCP server](https://apify.com/templates/python-mcp-empty) and [MCP proxy](https://apify.com/templates/python-mcp-proxy) templates
121121
122122
Whatever you build, the Apify SDK doesn't lock you into a particular framework. Bring the libraries you already use, and let Apify run your project in the cloud.
@@ -199,7 +199,7 @@ The full SDK documentation lives at **[docs.apify.com/sdk/python](https://docs.a
199199
| [Overview](https://docs.apify.com/sdk/python/docs/overview) | What the SDK is, what Actors are, and how the pieces fit together. |
200200
| [Quick start](https://docs.apify.com/sdk/python/docs/quick-start) | Create, run, and deploy your first Python Actor. |
201201
| [Concepts](https://docs.apify.com/sdk/python/docs/concepts/actor-lifecycle) | Actor lifecycle, input, storages, events, proxy management, interacting with other Actors, webhooks, accessing the Apify API, logging, configuration, and pay-per-event. |
202-
| [Guides](https://docs.apify.com/sdk/python/docs/guides/beautifulsoup-httpx) | Integrations with BeautifulSoup, Parsel, Playwright, Selenium, Crawlee, Scrapy, Scrapling, Crawl4AI, and Browser Use, plus running a web server and using uv. |
202+
| [Guides](https://docs.apify.com/sdk/python/docs/guides/beautifulsoup-httpx) | Integrations with BeautifulSoup, Parsel, Playwright, Selenium, Crawlee, Scrapy, Scrapling, Crawl4AI, and Browser Use, plus building AI agents, running a web server, and using uv. |
203203
| [Upgrading](https://docs.apify.com/sdk/python/docs/upgrading/upgrading-to-v4) | Migrating between major versions. |
204204
| [API reference](https://docs.apify.com/sdk/python/reference) | Generated reference for every class and method. |
205205
| [Changelog](https://docs.apify.com/sdk/python/docs/changelog) | Release history and breaking changes. |

docs/01_introduction/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Almost any Python project can become an Actor, including projects for:
4141
- **Web scraping and crawling** - The SDK is fully compatible with [Crawlee](https://crawlee.dev/python), which makes Apify a natural place to deploy and scale your crawlers (see the [Crawlee guide](../guides/crawlee)). It also works with other popular scraping libraries, such as [Scrapy](../guides/scrapy), [Scrapling](../guides/scrapling), or [Crawl4AI](../guides/crawl4ai).
4242
- **Browser automation** - Drive a real browser with [Playwright](../guides/playwright) or [Selenium](../guides/selenium), or with higher-level tools such as [Browser Use](../guides/browser-use).
4343
- **Web servers and APIs** - Run a [web server](../guides/running-webserver) inside an Actor to serve HTTP requests, for example to expose your scraper as a live API.
44-
- **AI agents** - Host agents built with your framework of choice. Ready-made Actor templates cover [PydanticAI](https://apify.com/templates/python-pydanticai), [CrewAI](https://apify.com/templates/python-crewai), [LangGraph](https://apify.com/templates/python-langgraph), [LlamaIndex](https://apify.com/templates/python-llamaindex-agent), and [Smolagents](https://apify.com/templates/python-smolagents).
44+
- **AI agents** - Host agents built with your framework of choice (see the [AI agents guide](../guides/ai-agents)). Ready-made Actor templates cover [PydanticAI](https://apify.com/templates/python-pydanticai), [CrewAI](https://apify.com/templates/python-crewai), [LangGraph](https://apify.com/templates/python-langgraph), [LlamaIndex](https://apify.com/templates/python-llamaindex-agent), and [Smolagents](https://apify.com/templates/python-smolagents).
4545
- **MCP servers** - Deploy a Python MCP server as an Actor and make its tools available to any MCP client. See the [MCP server](https://apify.com/templates/python-mcp-empty) and [MCP proxy](https://apify.com/templates/python-mcp-proxy) templates.
4646

4747
Whatever you build, the Apify SDK doesn't lock you into a particular framework. Bring the libraries you already use, and let Apify run your project in the cloud.

docs/01_introduction/quick-start.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ To see how you can integrate the Apify SDK with popular scraping libraries and f
111111
- [Adaptive scraping with Scrapling](../guides/scrapling)
112112
- [LLM-ready scraping with Crawl4AI](../guides/crawl4ai)
113113
- [Browser AI agents with Browser Use](../guides/browser-use)
114+
- [Building AI agents](../guides/ai-agents)
114115

115116
For other aspects of Actor development, explore these guides:
116117

docs/03_guides/13_ai_agents.mdx

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
id: ai-agents
3+
title: Building AI agents
4+
description: Host AI agents built with PydanticAI, CrewAI, LangGraph, LlamaIndex, or Smolagents as Apify Actors.
5+
---
6+
7+
import RunnableCodeBlock from '@site/src/components/RunnableCodeBlock';
8+
9+
import AiAgentsExample from '!!raw-loader!roa-loader!./code/13_ai_agents.py';
10+
11+
In this guide, you'll learn how to host an AI agent as an Apify Actor, using the agent framework of your choice.
12+
13+
## Introduction
14+
15+
An AI agent is a program that uses a large language model (LLM) to decide what to do next. Instead of following a fixed script, it reasons about a task, calls tools to gather information or take actions, and loops until the task is done. The Python ecosystem offers several frameworks for building such agents, and the Apify SDK doesn't lock you into any of them.
16+
17+
Apify Actors are a natural home for agents:
18+
19+
- An agent is just a Python program, so wrapping it in `async with Actor:` is all it takes to run it on the platform.
20+
- The platform scales runs, schedules them, and keeps logs, so you don't operate any infrastructure yourself.
21+
- The [Apify Store](https://apify.com/store) gives your agent thousands of ready-made tools. Any Actor (a scraper, a browser automation, an API wrapper) can become a tool the agent calls.
22+
- The [Apify OpenRouter proxy](https://apify.com/apify/openrouter) lets the agent talk to an LLM without managing a provider API key.
23+
- Pay-per-event charging lets you monetize the agent and pass LLM costs on to its users.
24+
25+
## Choosing a framework
26+
27+
Apify maintains a ready-made Actor template for each of the popular agent frameworks. Each template is a complete, deployable Actor that you can use as a starting point. Pick the one that matches the framework you already use, or the style of agent you want to build:
28+
29+
| Framework | Good for | Template |
30+
|---|---|---|
31+
| [PydanticAI](https://ai.pydantic.dev/) | Typed, Pydantic-native agents with tool calling | [`python-pydanticai`](https://apify.com/templates/python-pydanticai) |
32+
| [CrewAI](https://www.crewai.com/) | Multi-agent "crews" that collaborate on a task | [`python-crewai`](https://apify.com/templates/python-crewai) |
33+
| [LangGraph](https://www.langchain.com/langgraph) | Graph-based agents with explicit state and control flow | [`python-langgraph`](https://apify.com/templates/python-langgraph) |
34+
| [LlamaIndex](https://www.llamaindex.ai/) | Retrieval-augmented agents over your own data | [`python-llamaindex-agent`](https://apify.com/templates/python-llamaindex-agent) |
35+
| [Smolagents](https://github.com/huggingface/smolagents) | Lightweight code-writing agents from Hugging Face | [`python-smolagents`](https://apify.com/templates/python-smolagents) |
36+
37+
All of these templates live in the [actor-templates repository](https://github.com/apify/actor-templates), and you can scaffold any of them with the [Apify CLI](https://docs.apify.com/cli), for example `apify create my-agent --template python-pydanticai`.
38+
39+
## Example Actor
40+
41+
The following Actor runs a [PydanticAI](https://ai.pydantic.dev/) agent for a single prompt and stores the answer in the default dataset. The prompt and model are configurable through the Actor input.
42+
43+
A `build_agent` helper holds the PydanticAI-specific setup, while the `main` coroutine handles the [Actor](https://docs.apify.com/platform/actors) lifecycle, reads the input, runs the agent, and stores the result:
44+
45+
<RunnableCodeBlock className="language-python" language="python">
46+
{AiAgentsExample}
47+
</RunnableCodeBlock>
48+
49+
Note that:
50+
51+
- Keeping the agent setup in `build_agent` separates the framework-specific code from the Actor's orchestration logic. `main` only decides what to read from the input and what to store.
52+
- The agent talks to its LLM through the Apify OpenRouter proxy, so the Actor needs no provider API key. See [Connecting to an LLM](#connecting-to-an-llm) below.
53+
- `output_type=str` returns the answer as plain text. Pass a [Pydantic](./input-validation) model instead to get a validated object that maps onto a dataset row.
54+
55+
## Connecting to an LLM
56+
57+
Every agent needs an LLM, and there are two ways to provide one.
58+
59+
The example above uses the [Apify OpenRouter proxy](https://apify.com/apify/openrouter), an OpenAI-compatible endpoint at `https://openrouter.apify.actor/api/v1` that fronts the full [OpenRouter](https://openrouter.ai) model catalog. The token usage is billed against the Apify account running the Actor, so no provider API key is required. The Actor authenticates with the proxy using the `APIFY_TOKEN` that the platform injects into every run. To switch models, change the `model` input to any [OpenRouter model slug](https://openrouter.ai/models), for example `openai/gpt-4o-mini`.
60+
61+
Alternatively, you can call a provider such as OpenAI, Anthropic, or Google directly with your own API key. Keep the key out of the Actor input and source code. Read it from an environment variable, which on the platform you set as a [secret environment variable](https://docs.apify.com/platform/actors/development/programming-interface/environment-variables) and locally you export in your shell.
62+
63+
## Using Apify Actors as tools
64+
65+
An agent becomes useful once it can act on the world. On Apify, any of the thousands of Actors in the [Apify Store](https://apify.com/store) can become a tool: a scraper that fetches data, a browser automation that fills a form, or an API wrapper that posts a message.
66+
67+
For LangChain-based frameworks, such as LangGraph and CrewAI, the [`langchain-apify`](https://github.com/apify/langchain-apify) package exposes any Actor as a tool with a single line:
68+
69+
```python
70+
from crewai_tools import ApifyActorsTool
71+
72+
# Let the agent scrape Instagram profiles by calling the apify/instagram-scraper Actor.
73+
tools = [ApifyActorsTool('apify/instagram-scraper')]
74+
```
75+
76+
For any other framework, call the Actor directly through the Actor's preconfigured API client and read its dataset:
77+
78+
```python
79+
@tool
80+
async def scrape_instagram(handle: str) -> list[dict]:
81+
"""Scrape recent posts from an Instagram profile."""
82+
run_input = {'directUrls': [f'https://www.instagram.com/{handle}/']}
83+
run = await Actor.apify_client.actor('apify/instagram-scraper').call(run_input=run_input)
84+
return (await Actor.apify_client.dataset(run['defaultDatasetId']).list_items()).items
85+
```
86+
87+
For details on calling other Actors, see [Interacting with other Actors](../concepts/interacting-with-other-actors).
88+
89+
## Other frameworks
90+
91+
The same `async with Actor:` pattern wraps an agent built with any framework. The snippets below show how each one defines an agent. For a full, deployable Actor, start from the matching template.
92+
93+
[**CrewAI**](https://apify.com/templates/python-crewai) assembles one or more role-playing agents into a crew that works through tasks:
94+
95+
```python
96+
from crewai import Agent, Crew, Task
97+
98+
agent = Agent(
99+
role='Social Media Analyst',
100+
goal='Analyze Instagram profiles and summarize the findings.',
101+
backstory='An expert at turning social media data into insights.',
102+
tools=[ApifyActorsTool('apify/instagram-scraper')],
103+
llm='gpt-4o-mini',
104+
)
105+
task = Task(description=query, expected_output='A short report.', agent=agent)
106+
result = (await Crew(agents=[agent], tasks=[task]).kickoff_async()).raw
107+
```
108+
109+
[**LangGraph**](https://apify.com/templates/python-langgraph) builds an agent as a graph with explicit state, which makes complex control flow easy to follow:
110+
111+
```python
112+
from langchain.agents import create_agent
113+
from langchain_openai import ChatOpenAI
114+
115+
agent = create_agent(ChatOpenAI(model='gpt-4o-mini'), tools=[scrape_instagram])
116+
result = await agent.ainvoke({'messages': [('user', query)]})
117+
```
118+
119+
[**LlamaIndex**](https://apify.com/templates/python-llamaindex-agent) shines at retrieval-augmented agents that reason over your own data:
120+
121+
```python
122+
from llama_index.core.agent import ReActAgent
123+
from llama_index.core.tools import FunctionTool
124+
from llama_index.llms.openai import OpenAI
125+
126+
agent = ReActAgent(
127+
tools=[FunctionTool.from_defaults(fn=scrape_instagram)],
128+
llm=OpenAI(model='gpt-4o-mini'),
129+
)
130+
response = await agent.run(user_msg=query)
131+
```
132+
133+
[**Smolagents**](https://apify.com/templates/python-smolagents) is a lightweight framework from Hugging Face whose agents write and run Python code to solve a task:
134+
135+
```python
136+
from smolagents import CodeAgent, OpenAIServerModel, WebSearchTool
137+
138+
agent = CodeAgent(tools=[WebSearchTool()], model=OpenAIServerModel(model_id='gpt-4o-mini'))
139+
result = agent.run(query)
140+
```
141+
142+
## Running on the Apify platform
143+
144+
Agents run on the standard [Apify Python base image](https://hub.docker.com/r/apify/actor-python), so no browser or extra system dependencies are needed. Add `apify` and your framework to `requirements.txt`, for example:
145+
146+
```text
147+
apify
148+
pydantic-ai
149+
```
150+
151+
To monetize the agent, use [pay-per-event charging](../concepts/pay-per-event). You define events such as `task-completed` in the Actor's monetization settings and trigger them from the code:
152+
153+
```python
154+
await Actor.charge('task-completed')
155+
```
156+
157+
This lets you charge users directly from the Actor and cover the cost of execution and LLM tokens. It pairs naturally with the Apify OpenRouter proxy, which already bills the LLM usage to the account running the Actor.
158+
159+
## Conclusion
160+
161+
In this guide, you learned how to host an AI agent as an Apify Actor. You can now run an agent built with PydanticAI, CrewAI, LangGraph, LlamaIndex, or Smolagents, connect it to an LLM through the Apify OpenRouter proxy, give it Apify Actors as tools, and monetize it with pay-per-event. To get started, see the [Actor templates](https://apify.com/templates/categories/python). If you have questions or need assistance, feel free to reach out on our [GitHub](https://github.com/apify/apify-sdk-python) or join our [Discord community](https://discord.com/invite/jyEM2PRvMU). Happy building!
162+
163+
## Additional resources
164+
165+
- [Apify templates: PydanticAI](https://apify.com/templates/python-pydanticai)
166+
- [Apify templates: CrewAI](https://apify.com/templates/python-crewai)
167+
- [Apify templates: LangGraph](https://apify.com/templates/python-langgraph)
168+
- [Apify templates: LlamaIndex](https://apify.com/templates/python-llamaindex-agent)
169+
- [Apify templates: Smolagents](https://apify.com/templates/python-smolagents)
170+
- [Apify: actor-templates repository](https://github.com/apify/actor-templates)
171+
- [Apify: OpenRouter proxy](https://apify.com/apify/openrouter)
172+
- [Apify: langchain-apify integration](https://github.com/apify/langchain-apify)
173+
- [Apify blog: What are AI agents](https://blog.apify.com/what-are-ai-agents/)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import asyncio
2+
import os
3+
4+
from pydantic_ai import Agent
5+
from pydantic_ai.models.openai import OpenAIChatModel
6+
from pydantic_ai.providers.openai import OpenAIProvider
7+
8+
from apify import Actor
9+
10+
# The agent reaches its LLM through the Apify OpenRouter proxy, an
11+
# OpenAI-compatible endpoint billed against the run's Apify account. It
12+
# authenticates with the `APIFY_TOKEN` that the platform injects into every run,
13+
# so no separate provider API key (such as `OPENAI_API_KEY`) is needed.
14+
OPENROUTER_BASE_URL = 'https://openrouter.apify.actor/api/v1'
15+
16+
DEFAULT_PROMPT = 'In two sentences, explain what an Apify Actor is.'
17+
18+
19+
def build_agent(model_name: str) -> Agent[None, str]:
20+
"""Build a PydanticAI agent that routes LLM calls through Apify OpenRouter."""
21+
provider = OpenAIProvider(
22+
base_url=OPENROUTER_BASE_URL,
23+
api_key=os.environ['APIFY_TOKEN'],
24+
)
25+
return Agent(
26+
OpenAIChatModel(model_name, provider=provider),
27+
output_type=str,
28+
system_prompt='You are a concise, helpful research assistant.',
29+
)
30+
31+
32+
async def main() -> None:
33+
async with Actor:
34+
# Read the Actor input.
35+
actor_input = await Actor.get_input() or {}
36+
prompt = actor_input.get('prompt', DEFAULT_PROMPT)
37+
model_name = actor_input.get('model', 'openai/gpt-4o-mini')
38+
39+
# Build the agent and run it for a single prompt.
40+
Actor.log.info(f'Running the agent (model={model_name}) for: {prompt}')
41+
agent = build_agent(model_name)
42+
result = await agent.run(user_prompt=prompt)
43+
44+
# Store the agent's answer in the default dataset.
45+
Actor.log.info(f'The agent responded:\n{result.output}')
46+
await Actor.push_data({'prompt': prompt, 'response': result.output})
47+
48+
49+
if __name__ == '__main__':
50+
asyncio.run(main())

0 commit comments

Comments
 (0)