You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this guide, you'll learn how to host an AI agent as an Apify Actor, using the agent framework of your choice.
12
16
@@ -28,37 +32,80 @@ Apify maintains a ready-made Actor template for each of the popular agent framew
28
32
29
33
| Framework | Good for | Template |
30
34
|---|---|---|
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)|
35
+
|[PydanticAI](#pydanticai)| Typed, Pydantic-native agents with tool calling |[`python-pydanticai`](https://apify.com/templates/python-pydanticai)|
36
+
|[CrewAI](#crewai)| Multi-agent "crews" that collaborate on a task |[`python-crewai`](https://apify.com/templates/python-crewai)|
37
+
|[LangGraph](#langgraph)| Graph-based agents with explicit state and control flow |[`python-langgraph`](https://apify.com/templates/python-langgraph)|
38
+
|[LlamaIndex](#llamaindex)| Retrieval-augmented agents over your own data |[`python-llamaindex-agent`](https://apify.com/templates/python-llamaindex-agent)|
39
+
|[Smolagents](#smolagents)| Lightweight code-writing agents from Hugging Face |[`python-smolagents`](https://apify.com/templates/python-smolagents)|
36
40
37
41
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
42
39
-
## Example Actor
43
+
## Connecting to an LLM
44
+
45
+
Every agent needs an LLM, and there are two ways to provide one.
46
+
47
+
The first is 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. Any framework with an OpenAI-compatible client can point its base URL at the proxy, which is what most examples below do. To switch models, change the `model` input to any [OpenRouter model slug](https://openrouter.ai/models), for example `openai/gpt-4o-mini`.
40
48
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.
49
+
The second is to call a provider such as OpenAI directly with your own API key, which the CrewAI example below does through LiteLLM. 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.
42
50
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:
51
+
Each section below shows a complete, single-file Actor for one framework. They all read the input, run the agent, and store the result in the default dataset.
52
+
53
+
## PydanticAI
54
+
55
+
[PydanticAI](https://ai.pydantic.dev/) is an agent framework from the team behind Pydantic. It is strongly typed and integrates naturally with the [Pydantic models](./input-validation) the Apify SDK already uses. The following Actor runs an agent for a single prompt:
Set `output_type` to a Pydantic model instead of `str` to get a validated object back, which maps directly onto a dataset row.
62
+
63
+
## CrewAI
64
+
65
+
[CrewAI](https://www.crewai.com/) models a problem as a "crew" of role-playing agents that work through tasks. The following Actor defines a single analyst agent and one task:
-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.
73
+
-`kickoff_async` runs the crew without blocking the Actor's event loop.
74
+
-CrewAI calls the LLM through [LiteLLM](https://docs.litellm.ai/), so this example reads `OPENAI_API_KEY`. To route it through the Apify OpenRouter proxy instead, configure a custom `LLM` (see the [CrewAI LLM docs](https://docs.crewai.com/concepts/llms)).
75
+
-On a fresh container, crewAI shows a one-time trace-consent prompt. The template sets `CREWAI_TESTING=true` to suppress it; do the same in your Dockerfile.
54
76
55
-
## Connecting to an LLM
77
+
## LangGraph
56
78
57
-
Every agent needs an LLM, and there are two ways to provide one.
79
+
[LangGraph](https://www.langchain.com/langgraph) builds an agent as a graph with explicit state, which makes complex, multi-step control flow easy to follow. It builds on [LangChain](https://www.langchain.com/), so any LangChain chat model and tool works. The following Actor runs a single-turn agent:
The example passes an empty `tools` list for brevity. Add LangChain tools to give the agent abilities, and read the final answer from the last message in the returned state.
86
+
87
+
## LlamaIndex
88
+
89
+
[LlamaIndex](https://www.llamaindex.ai/) is built for retrieval-augmented agents that reason over your own data. The following Actor runs a `ReActAgent` with a single tool:
-`OpenAILike` is the LlamaIndex LLM class for OpenAI-compatible endpoints such as the Apify OpenRouter proxy. It needs the `llama-index-llms-openai-like` package.
98
+
- The agent decides on its own when to call the `word_count` tool. Add `FunctionTool`s of your own to extend it.
58
99
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`.
100
+
## Smolagents
60
101
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.
102
+
[Smolagents](https://github.com/huggingface/smolagents) is a lightweight framework from Hugging Face whose agents write and run Python code to solve a task. The following Actor runs a `CodeAgent`:
A `CodeAgent` executes the Python code it generates, so run it in the isolated Actor container rather than on your own machine. Pass tools such as `WebSearchTool` in the `tools` list to let it gather information.
For details on calling other Actors, see [Interacting with other Actors](../concepts/interacting-with-other-actors).
88
134
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.',
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:
[**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
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 frameworkto `requirements.txt`, for example:
137
+
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's packages to `requirements.txt`:
145
138
146
-
```text
147
-
apify
148
-
pydantic-ai
149
-
```
139
+
- PydanticAI: `pydantic-ai`
140
+
- CrewAI: `crewai` (or `crewai[tools]` for `ApifyActorsTool`)
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
146
@@ -158,7 +152,7 @@ This lets you charge users directly from the Actor and cover the cost of executi
158
152
159
153
## Conclusion
160
154
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!
155
+
In this guide, you learned how to host an AI agent as an Apify Actor. You can now build an agent 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!
0 commit comments