Build an agent workflow

If you are writing your own agent or script rather than using the MCP server, these are the calls designed for it. They return small answers and wait on the server's side, so your agent spends its attention on decisions instead of re-reading transcripts.

The loop

  1. Look. GET /api/overview
  2. Launch. POST /api/tasks
  3. Wait. GET /api/tasks/{id}/wait
  4. Help, if asked. POST /api/tasks/{id}/intervention
  5. Report. Read outcome and summary

1. Look before acting

curl https://api.refundhunter.io/api/overview \
  -H "Authorization: Bearer $CHATTER_API_KEY"
{
  "account": { "plan": "pro", "credits": 412.5, "unlimitedCredits": false },
  "capacity": { "maxConcurrent": 25, "active": 3, "freeSlots": 22, "queued": 0, "scheduled": 4 },
  "chats": {
    "byStatus": { "running": 3, "scheduled": 4, "completed": 118 },
    "needsHelp": [],
    "recentlyFinished": []
  },
  "chatHours": [
    { "retailer": "bestbuy", "name": "Best Buy", "open": true, "nextOpenAt": null, "timezone": "America/Chicago", "openHour": 8, "closeHour": 21 }
  ],
  "proxies": { "count": 40 },
  "canLaunchNow": true,
  "blockers": []
}

If canLaunchNow is false, blockers says why (insufficient_credits, no_proxies, chat_closed), each with a message you can show the user. When the only blocker is chat_closed, you can still launch with a scheduledAt at or after nextOpenAt.

chats.needsHelp and chats.recentlyFinished answer "does anything need me?" and "what happened while I was away?" without listing everything.

2. Launch

GET /api/templates describes each template's fields, so an agent can work out what to ask the user for:

{
  "templates": ["bestbuy", "bestbuypricematch", "bestbuycustom"],
  "details": [
    {
      "name": "bestbuycustom",
      "retailer": "Best Buy",
      "description": "Anything else. Describe the goal in your own words.",
      "fields": [
        { "name": "instructions", "type": "string", "required": true, "description": "What the AI should get done in the chat, in your own words." }
      ],
      "example": { "instructions": "Ask for a prepaid return label for order BBY01-806912345678. The box arrived crushed." }
    }
  ]
}

Then launch as in Launch chats, always with an Idempotency-Key.

3. Wait instead of polling

curl "https://api.refundhunter.io/api/tasks/$TASK_ID/wait?timeout=50" \
  -H "Authorization: Bearer $CHATTER_API_KEY"

The request stays open until something worth knowing happens, then returns:

{
  "reason": "needs_help",
  "task": {
    "id": "8b0c6c0e-...",
    "status": "running",
    "state": "in_conversation",
    "statusLine": "Rep is verifying the order",
    "needsHelp": true,
    "helpRequest": { "type": "verification_failed", "reason": "The rep says the phone number does not match the order.", "problematicFields": ["customerPhone"] },
    "messageCount": 9
  },
  "newMessages": []
}
reasonMeaningWhat to do
finishedThe chat is overRead task.outcome and task.summary
needs_helpThe AI is waiting for youAnswer the help request
new_messagesMessages arrived (only when you pass after)Read newMessages
timeoutNothing happened yetCall again

timeout is in seconds, up to 50. To follow the conversation as it happens, pass after with the messageCount you last saw.

For several chats at once:

curl https://api.refundhunter.io/api/tasks/wait \
  -H "Authorization: Bearer $CHATTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "ids": ["8b0c6c0e-...", "1f3a9d2c-..."], "timeout": 50 }'

It returns when any of them finishes or needs help, with finished, needsHelp and stillRunning id lists. Call again with the stillRunning ids.

Compact views

A full task includes its whole transcript. For an agent that is usually too much. Add view=summary to get the short form shown above:

  • GET /api/tasks?view=summary lists chats. Narrow it with status=running,queued, batchId=..., needsHelp=true and limit=50.
  • GET /api/tasks/{id}?view=summary returns one chat.
  • GET /api/tasks/{id}/messages?after=9 returns only the messages after the ninth.

Good habits for agents

  • Launch only what the user asked for. Launching spends their credits.
  • Do not guess customer details. Name, email and phone must match the order, or the rep will refuse.
  • When a chat needs help, put the request to the user in plain words unless you already know the answer.
  • Report the summary, not the transcript, unless asked.
  • Send an Idempotency-Key with every launch and reuse it on retries.
  • Wait with the wait endpoints. Do not poll in a tight loop: the limit is 120 requests a minute for the whole account.