Skip to main content
Convert successful agent runs into deterministic function code that can be executed faster and cheaper than agents.

Overview

After an agent completes a task, you can extract the steps as function code:

Why Convert to Functions?

Use functions when:
  • The task is well-defined and repeatable
  • Page structure is stable
  • Speed and cost matter
  • You need predictable execution

Getting Function Code

As Python Script

Get executable Python code:
get-python-script.py
Example output:

As Function Object

Create a reusable function:
create-function-object.py

Running Functions

Execute Generated Code

Copy and run the generated Python:

Use Function API

Run via the function endpoint:
use-function-api.py

Use Cases

1. Prototyping with Agents

Use agents to figure out the automation, then convert:
prototyping-with-agents.py

2. Scheduled Tasks

Convert one-time agent runs to scheduled functions:
scheduled-tasks.py

3. Cost Optimization

Run agents once, then use cheaper functions:
cost-optimization.py

4. Testing Variations

Create function templates from agents:
testing-variations.py

Customizing Generated Code

Add Parameters

Make functions reusable with parameters:
add-parameters.py

Add Error Handling

Enhance with production-ready error handling:
add-error-handling.py

Optimize Selectors

Review and improve generated selectors:
optimize-selectors.py

Best Practices

1. Test Generated Functions

Always test before production:
test-generated-functions.py

2. Document the Source

Track which agent generated the function:
document-the-source.py

3. Version Control

Store functions in git:
version-control.py

4. Monitor Function Success

Track if functions continue to work:

5. Regenerate When Pages Change

When functions break, use agents to update:
regenerate-when-pages-change.py

Limitations

Functions work best for:
  • ✅ Stable, unchanging pages
  • ✅ Deterministic tasks
  • ✅ Known sequences of actions
Functions are not suitable for:
  • ❌ Pages that change frequently
  • ❌ Tasks requiring adaptation
  • ❌ Complex decision-making
When pages change or tasks become complex, revert to agents.

Next Steps

Functions

Deploy automations as serverless APIs

Actions

Available session actions

Agent Configuration

Optimize agent runs

Functions

Deploy and manage serverless functions