Overview
After an agent completes a task, you can extract the steps as function code:Why Convert to Functions?
| Aspect | Agent | Function |
|---|---|---|
| Execution | AI decides each step | Predefined steps |
| Speed | Slower (LLM reasoning) | Faster (direct execution) |
| Cost | Higher (LLM tokens) | Lower (actions only) |
| Reliability | Can vary | Deterministic |
| Adaptability | Handles changes | Breaks if page changes |
- 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
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
- ❌ Pages that change frequently
- ❌ Tasks requiring adaptation
- ❌ Complex decision-making

