Operations
Deployment
From a working local integration to one that auto-syncs on every push, without breaking the first deploy.
8 min read
AgentGraft is config-as-code. That principle is great for code review and rollback, but it creates one operational wrinkle: how do you make sure the config that's running in production is the same as what's checked in? The answer is to wire agentgraft_sync into your deploy pipeline so it runs on every push, automatically, without anyone needing to remember it.
#The pre-deploy chain
On Railway, the cleanest place is the preDeployCommand in your railway.json. It runs after the build but before the new container takes over, so a sync failure blocks the deploy and the old container keeps serving uninterrupted. The same pattern works for any platform with a release-phase hook (Heroku, Fly, render.com).
{
"$schema": "https://railway.com/railway.schema.json",
"build": {
"builder": "RAILPACK",
"buildCommand": "uv sync --frozen --no-dev && python manage.py collectstatic --noinput"
},
"deploy": {
"preDeployCommand": "python manage.py migrate && python manage.py agentgraft_sync --skip-if-unconfigured",
"startCommand": "gunicorn myapp.wsgi --bind 0.0.0.0:$PORT --timeout 120"
}
}
#Why --skip-if-unconfigured
Without this flag, the very first deploy of a new project is broken by design: the Railway environment doesn't have any AGENTGRAFT_* env vars set yet, the sync command fails on the missing credentials, the deploy fails and the service never starts, though nothing about your app is wrong yet.
Read the variables with os.environ.get(..., "") in settings.py, as the Quickstart does: os.environ[...] raises before any command runs, and no flag can save that deploy.
--skip-if-unconfigured makes the sync command quietly no-op on profiles that don't have credentials yet, logging a warning to the deploy log so the operator can see exactly what was skipped. The first deploy succeeds, the service comes up without a working chat widget (acceptable transitional state), the operator signs up at agentgraft.com/accounts/register/, confirms the emailed link, copies the credentials into Railway env vars, and the next deploy syncs the config.
It skips a profile whose api_base, api_key or graft_id is unset, and one whose registry is empty, and nothing else: a sync the server refuses (a mistyped policy, a webhook URL that isn't public) still fails the deploy, as it should. --retries N (default 3) retries the push and policy fetches on network errors, 429 and 5xx; --dry-run prints the payload without sending it.
- 1First push: build succeeds, migrations run, sync runs and skips the profile (no credentials), service starts, the chat widget renders as an HTML comment instead of a script tag.
- 2Sign up at
https://agentgraft.com/accounts/register/and open the confirmation link we email you; it works once, for an hour. Confirming shows your default graft's credentials exactly once. Save them. - 3Paste the five values into Railway env vars:
AGENTGRAFT_API_BASE,AGENTGRAFT_GRAFT_ID,AGENTGRAFT_PUBLIC_KEY,AGENTGRAFT_API_KEY,AGENTGRAFT_WEBHOOK_SECRET, plusAGENTGRAFT_WEBHOOK_URL(your app's public webhook URL). - 4Click Redeploy in Railway (no code change required). The same
preDeployCommandre-runs, the sync this time finds credentials, pushes your config, and the chat widget appears on the next page load.
#Force a re-sync without a code change
Sometimes you want to push the current config without shipping new code — most commonly when AgentGraft has improved a Policy Shop policy you reference (its new text reaches your graft only on your next sync), or when you're rolling back a problematic config change.
- The Redeploy button: Railway's dashboard has a Redeploy action that re-runs the entire build + release phases against the current commit. Same
preDeployCommand, same sync. Zero git operations. - Manual sync from your dev machine: with production's values in your environment (
AGENTGRAFT_API_BASE=https://agentgraft.comand that graft's id, API key and publicwebhook_url), runpython manage.py agentgraft_sync. No deploy at all.
#Railway secrets you'll need
Beyond the AgentGraft credentials, the host app needs the standard Django production secrets. Here's the minimum set for a typical deploy:
SECRET_KEY=... # generated, never reused
DATABASE_URL=postgres://... # provided by your db addon
REDIS_URL=redis://... # only if your app uses Redis itself
ALLOWED_HOSTS=yourapp.com
AGENTGRAFT_API_BASE=https://agentgraft.com
AGENTGRAFT_GRAFT_ID=018f3a7c-...
AGENTGRAFT_PUBLIC_KEY=pk_live_...
AGENTGRAFT_API_KEY=sk_live_...
AGENTGRAFT_WEBHOOK_SECRET=whsec_...
AGENTGRAFT_WEBHOOK_URL=https://yourapp.com/agentgraft/webhook/
AGENTGRAFT_API_BASE has no default: the sync, the widget tag and Client all need it. It is https://agentgraft.com unless you run AgentGraft yourself.
Read webhook_url from an environment variable, so staging and production each push their own public URL. A sync from a dev machine must never push a localhost one, and the server refuses it anyway.
#What your webhook must allow
- A public address, the exact URL you set as
webhook_url: AgentGraft never follows a redirect, so an http→https,wwwor missing-slash redirect fails every tool call. - AgentGraft waits 10 s to connect and up to 120 s for a reply. Give your app server at least that (gunicorn:
--timeout 120), or keep tools fast. - A call is retried once, with the same
request_id, on connection errors and 502, 503 or 504, never after a timeout. Tools that write can useag.current_request_id()to skip work they already did. - Reply with at most 1 MiB of uncompressed JSON; don't let a proxy compress it.
- Keep your server's clock in sync: a call signed more than 300 s from your clock is refused as a replay.
- Leave the webhook outside login, basic auth and bot challenges: it verifies the HMAC itself and is exempt from CSRF.
#Rotating credentials
An owner or admin rotates a graft's API key or webhook secret from its page in the dashboard. Each one takes effect differently:
- A new API key replaces the old one at once. Update
AGENTGRAFT_API_KEYbefore the next sync, or that sync fails. - A new webhook secret signs tool calls at once, so your webhook refuses them until you redeploy with it.
- Keep the previous secret (a planned change) and it still verifies visitors on pages your app already rendered for 24 hours. Without it (the secret may have leaked), the old one stops at once.
#Running agents from your server
Run bots and scheduled jobs in a worker such as Celery, not in a web request. Client.send_message replies only once the whole turn is done: it waits up to 630 s, or budget_s + reply_s + 15 s with a deadline, and is never retried.
- Send a
turn_keywith each turn and retry with the same key: a finished turn sends back its saved reply instead of running again. - A 409 whose
APIError.codeisturn_unfinishedmeans the turn started and never finished, and nothing ran this time. Its tools may have done things; carry on with a new key. budget_s(at most 480 s) andreply_s(30 s by default, at most 60) give a turn a deadline, so a stuck run frees its conversation soon after.
#Verifying a deploy
After the deploy completes, the Railway logs for the preDeploy step show exactly what synced. A healthy run looks like this:
— Profile 'default' —
Sync complete.
Tools:
added: list_datasets, get_dataset
updated: search_assets
Agents:
added: helper
updated: router
webhook_url updated
graft fields updated: identity, voice
The webhook_url updated line appears when the sync changed the graft's webhook URL: on a new graft, the first sync that names one. If chats fail with "no webhook_url configured", your settings or ag.graft(...) never set it; set it and sync. An "unknown tool" error comes from your own app: it is running older code than the config you synced.
The graft's page in the dashboard also checks the webhook: it posts a test call and says whether your app answered, refused the signature (a stale webhook secret), redirected, or could not be reached.