Skip to main content

Synchronous vs Asynchronous

WebRun uses a hybrid response model that combines the simplicity of synchronous APIs with the reliability of asynchronous polling:
  • Tasks completing in < 50 seconds: Result returned inline (synchronous)
  • Tasks taking > 50 seconds: Poll URL returned for async polling
This design eliminates unnecessary polling for the 90% of tasks that complete quickly, while gracefully handling longer operations.

Why This Design?

Pure sync doesn’t work because HTTP timeouts (30-60s) are too short for complex browser tasks. Pure async doesn’t work because polling adds unnecessary complexity for simple tasks that finish in seconds. The hybrid approach waits up to 50 seconds. If the task finishes in time, the result comes back inline. If not, you get a poll URL. In practice, ~90% of tasks return inline.

How It Works


Inline Response (< 50 seconds)

When your task completes within 50 seconds, you get the full result in the initial response: Request:
Response:
Key indicator: type: "task_completed"

Pending Response (> 50 seconds)

For tasks that take longer than 50 seconds, you receive a pollUrl immediately: Request:
Response (after 50 seconds):
Key indicator: status: "pending" You must then poll the pollUrl until the task completes.

Polling for Results

Polling Endpoint

Poll Until Completion

Poll every 2-3 seconds until you receive a final state: Still Running:
Completed:
Guardrail Triggered:
Failed:

Polling Best Practices

1. Poll Interval

Poll every 2-3 seconds. Faster polling wastes resources; slower polling adds unnecessary latency.

2. Maximum Attempts

Set a reasonable timeout (e.g., 2 minutes = 60 attempts at 2-second intervals):

3. Handle All Terminal States

Check for all possible completion states:

4. Track Incremental Cost

The polling endpoint includes current cost in the usage object, allowing you to monitor spending in real-time.

Complete Polling Implementation


Unified Request Handler

Handle both inline and pending responses with a single function:

WebSocket Alternative

For real-time updates without polling, use WebSocket:
WebSocket removes the need for polling entirely by pushing updates as they occur. Learn more about WebSocket →

Response Time Distribution

Based on typical usage patterns: ~90% of tasks complete within 50 seconds and return inline.