Prerequisites
- Python 3.10+
- A Twexapi API key
- A Prefect deployment or local flow runner
- Storage for state such as
next_cursor,last_success_at, and selected endpoint path
Pattern
- Use MCP during planning and ad-hoc collection.
- Persist the selected endpoint path, query parameters, and pagination state.
- Run scheduled collection through Prefect tasks.
- Use Twexapi REST directly for deterministic scheduled runs.
Flow skeleton
MCP planning prompt
Handoff payload
Ask the planning agent to return this payload, then store it in Prefect variables or your own config.State to persist
| State | Purpose |
|---|---|
path | Stable endpoint chosen by the agent. |
query | Scheduled job parameters. |
next_cursor | Resume pagination. |
last_success_at | Recovery after failures. |
route_used | Audit trail. |
Deployment recipe
Plan with MCP
Ask an agent to use
explore and return the exact method, relative path, and parameters.Error handling
| Status | Prefect behavior |
|---|---|
401 | Fail fast and alert; the API key is missing or invalid. |
403 | Pause the deployment until credits or permissions are fixed. |
429 | Retry with delay and keep the same cursor. |
5xx | Retry the task; do not advance next_cursor until success. |
Testing checklist
- Run the flow locally with
count=5. - Verify logs include the selected
pathand request parameters. - Verify state persists after each successful page.
- Verify retries do not duplicate rows because
tweet_idoruser_idis used as a primary key.