> ## Documentation Index
> Fetch the complete documentation index at: https://docs.usehasp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Example: Multi-Agent Care Coordination

> A headless agent-callable workflow invoked over A2A from an external agent, authenticated with a delegated AgentCaller credential per ADR-3RWY4P.

This example walks through building a care coordination workflow that an AI agent or integration invokes over **A2A** on your org runtime. The narrative describes Dr. Patel as the clinical owner because the agent acts under delegated authority — audit and policy attribute the run to the delegating human.

## What you're building

| Resource                      | Description                                                     |
| ----------------------------- | --------------------------------------------------------------- |
| **Workflow** (Agent-callable) | Care coordination — invoked via A2A                             |
| **Entity**                    | `care_plan` — coordination record with status and next steps    |
| **Integration**               | EHR REST API (update patient chart)                             |
| **Integration**               | Scheduling system (book follow-up appointment)                  |
| **Integration**               | Slack (notify care team)                                        |
| **Credential**                | A delegated agent credential (`hasp_agt_*`) for `POST /a2a/...` |

## Step 1: Create the project

Click **New Project**. Name it *Care Coordination*.

## Step 2: Author the workflow

In chat:

> *"I need a workflow that an AI care coordination agent can invoke on Dr. Patel's behalf. The workflow should: look up the patient's current care plan, check for any open care gaps, book a follow-up appointment if there's a gap, update the EHR chart with the action taken, and notify the care team in Slack. The agent should be able to do this without Dr. Patel being at her keyboard."*

The system:

* Asks: *"I'll make this an agent-callable workflow — accessible to an `AgentCaller` acting under Dr. Patel's authority. Does that sound right?"* → Confirm.
* Creates a `care_plan` entity with fields: `patient_id`, `open_gaps`, `last_reviewed`, `next_appointment`, `status`
* Creates the care coordination workflow with trigger: agent invocation
* Compiles steps: look up care plan → check for open gaps → condition → book appointment (scheduling integration) → update chart (EHR integration) → notify care team (Slack integration)

## Step 3: Add the integrations

Add three integrations in **Settings → Integrations**:

1. **EHR API** — Custom HTTP, base URL: `https://ehr.acme-health.com/fhir/r4`, Bearer auth
2. **Scheduling** — Custom HTTP, base URL: `https://schedule.acme-health.com/api`, API key auth
3. **Slack** — Slack incoming webhook, workspace: *Acme Health*, channel: `#care-team`

## Step 4: Issue an agent credential

Register the agent and issue it a credential under Dr. Patel's authority — via **Settings → Agent Access** or the [Agents API](/ai-api/agents/registering-agents):

1. Register `CareCoordinatorAgent` and note its `agent_id`.
2. Issue a credential with an `expires_at` and the grants the automation needs — at minimum a [`workflow.invoke`](/ai-api/agents/scope-grants#workflowinvoke) grant naming this workflow, since A2A invocation requires it.
3. Copy the plaintext secret once (`hasp_agt_live_...`). Store it in your agent's secret manager.

<Note>
  The runtime also accepts a plain org API key (`hasp_api_live_*`), but that carries no delegating user, no expiry, and no per-issuance revocation handle — so the audit chain cannot record who the agent acted for. Prefer a delegated credential.
</Note>

## Step 5: Simulate an agent invocation

From the test/run console (the center pane for headless workflows), compose a trigger payload matching what you will later send as JSON-RPC **`params`** on A2A:

```json theme={null}
{
  "patient_id": "pat_01JA7QG2...",
  "context": "Post-discharge follow-up check"
}
```

Click **Run (Sandbox)**.

## Step 6: Inspect the trace

The trace shows:

1. `credential_validate` — credential valid and unexpired; org matches runtime subdomain; delegating user resolved
2. `entity_read` — looked up care plan for `pat_01JA7QG2`. Found 1 open care gap: annual diabetes screening overdue.
3. `condition` — `open_gaps > 0` → true
4. `book_appointment` — **sandbox:** *"would have booked follow-up appointment on 2026-05-15 at 10:00 AM."*
5. `update_ehr` — **sandbox:** *"would have updated EHR chart with note: Follow-up booked for diabetes screening."*
6. `entity_write` — care plan updated: `status: follow_up_scheduled`, `next_appointment: 2026-05-15`
7. `notify_slack` — **sandbox:** *"would have posted to #care-team: Patient \[anonymized] follow-up scheduled."*

All integration calls edge-sandboxed. No real calls made.

## Step 7: Release and connect the agent

After release, the workflow is live. Configure the `CareCoordinatorAgent` with a delegated agent credential (see [A2A protocol](/studio/a2a-protocol) and [Issuing credentials](/ai-api/agents/issuing-credentials)). Discover the exact JSON-RPC URL from `GET https://acme.usehasp.run/.well-known/agents.json`: each capability card includes an **`endpoint`** field — `POST` to that URL (not a separate `invocation_url` field).

```http theme={null}
POST https://acme.usehasp.run/a2a/care-coordination/care-coordination-run
Authorization: Bearer hasp_agt_live_<credential_secret>
Content-Type: application/json

{
  "jsonrpc": "2.0",
  "method": "invoke",
  "params": {
    "patient_id": "pat_01JA7QG2...",
    "context": "Post-discharge follow-up check"
  },
  "id": "req-001"
}
```

The run is attributed to the credential and, through it, to Dr. Patel as the delegating user; every step is audited. Every integration call is real in production mode.

## Audit trail

Every invocation produces an audit record:

* Org, `agent_id`, `credential_id`, delegating user, workflow, and run identifiers
* Full execution trace with step-level inputs and outputs
* EHR API payload and response (PHI-processed)
* Slack notification content (anonymized)
* Care plan entity mutation with before/after values

Dr. Patel can review all invocations from the Studio audit log. She can **revoke the credential** at any time — `kill` terminates in-flight runs, `drain` lets them finish and refuses new ones (see [Revocation](/ai-api/agents/revocation)).

## What changes between shifts

Issue a fresh credential per shift against the same `agent_id`: one persistent agent identity, many short-lived issuances. The agent configuration picks up the new secret; revoke the previous credential when the shift ends. Because `expires_at` is required, a forgotten credential expires on its own.

## Extending the example

* **Multi-agent delegation**: the `CareCoordinatorAgent` delegates a sub-task (e.g., medication reconciliation) to a `MedRecAgent` with a narrower scope credential — entity read only, no EHR write, no scheduling.
* **Escalation**: add a step that checks if the agent's scope is insufficient for the action required and escalates to a human via the on-call notification integration.
* **Approval gate**: for actions above a risk threshold, require a human approval step in the workflow before the EHR write executes.
