Catalog / Business processes / Revenue operations / Number-to-Territory / Model the Territories
reference process · revenue-operations · 15 activities · 10 on the roster
Model the Territories
Leadership sets a number, and somebody has to cut the market into patches a person can work. This process fixes what a territory is cut on, cleans the account base it will be cut from, sizes every account, and draws two or three candidate maps on the agreed rule. Each map is modelled for what its patches can carry and run backwards over what closed last year. Leaders pick one map and say why. The run ends with a map at a version and the assumption written beside every carrying number.
The walk
The document
One run
Adoption
The activities
What happens in a run
15 activities from leadership sets the number for the period to one map is fixed and every patch has a number it can carry, 2 of them gates a person has to sign. Drag the diagram to move along it.
Model the Territories leadership sets the number for the period → one map is fixed and every patch has a number it can carry
a field the rule reads is missing the model does not reproduce last year a patch nobody could work no cut on the table is workable leadership sets the number for the period 1 Take in the Number and th… the target, the segments, and the date the map is due 2 Fix What a Territory Is C… the rule the carve is drawn on, agreed once a person signs · never an agent 3 Take in the Account Base every account the carve will place, at one version 4 Clean the Account Record duplicates merged, unreadable fields named not guessed 5 Size Each Account what it buys today, and what it could carry 6 Read How Last Year Was Co… which patches were worked and which were never called 7 Draw the Candidate Cuts two or three maps, drawn on the agreed rule 8 Model What Each Patch Car… what a territory can produce in the period 9 Test the Model Against Hi… the model run backwards over what actually closed 10 Check Coverage and Balance no patch too big to work, none too small to live on 11 Cost Each Cut the sellers and the travel each map needs 12 Weigh the Cuts and Choose leaders pick one map and say why a person signs · never an agent 13 Fix the Territory Map the chosen map at a version, with its rules 14 Record What the Model Res… the assumption behind every carrying number 15 Hand the Map to Quota Set… the map, its rules, and what each patch carries one map is fixed and every patch has a number it can carry
The roster this process needs
Hover a name to see the activities it holds. A dashed one is a person, and stays one.
The document
The document, with the blanks marked
Everything in amber is yours to fill in: who owns it, when it takes effect, which platforms, which numbers, and who holds each activity. Everything else is the process, and it is the same wherever it is run.
Use in LangGraph
Use in Agent Framework
Use in CrewAI
Use in Google ADK
PROCESS: model the territories id: <team> /model-the-territories v1
from: ref/rev/model-the-territories v1
owner: <who> effective: <date>
trigger: the carve for <period> opens on <your planning calendar>
watch: record=<the carve for the period>
system=<your planning calendar>
change=<the carve opens for the period>
concurrency: one run per period per market - a re-cut inside the period
is a new version of the same run, not a new run
goal: every account in exactly one territory, every territory modelled
for what it can carry, and one map fixed at a version before any
quota is set against it
phases:
take-in-number - human: the target for the period, the segments it
covers, and the date the map has to be fixed
owner: territory-modeler after: trigger
automation: assisted
fix-carve-rule - convenes decide-and-announce: what a territory is
cut on, whether that is geography, industry, size
or named accounts, decided once and written down
before anything is drawn
owner: <your revenue operations role>
after: take-in-number automation: never
take-in-accounts - runs collect-and-report: every account the carve
will place, with the fields the rule reads,
pulled at one version of <your account record>
owner: analytics after: take-in-number
by: <days> automation: autonomous
clean-accounts - human: duplicates merged into one account, and
every account whose fields the rule cannot read
listed by name rather than filled in by guess
owner: <your data steward role>
after: take-in-accounts
by: <days> automation: assisted
size-accounts - runs assessment: what each account buys today and
what it could carry, each figure naming the
system it was read from
owner: account-monitor after: clean-accounts
by: <days> automation: supervised
read-coverage - runs collect-and-report: which patches were
worked last period, which were never called, and
what was left with no owner at all
owner: coverage-check
after: take-in-accounts automation: autonomous
draw-candidates - runs build-by-talent: <how many> maps drawn on
the agreed rule, each one placing every account
exactly once
owner: territory-modeler
after: fix-carve-rule + size-accounts +
read-coverage
by: <days> automation: supervised
model-carrying - runs build-by-talent: what each patch on each map
can produce in the period, with the assumption
written beside every number
owner: territory-modeler after: draw-candidates
by: <days> automation: supervised
test-history - runs assessment: the model run backwards over the
last <periods> and set beside what those patches
actually closed
owner: statistician after: model-carrying
automation: supervised
check-balance - runs assessment: no patch above <accounts>
accounts or above <amount> , none below <amount> ,
and no account sitting in two patches
owner: coverage-check after: test-history
automation: autonomous
cost-cuts - runs allocate-and-reconcile: the sellers, the
travel and the support each map needs, priced
against <your cost model>
owner: capacity-manager after: check-balance
by: <days> automation: supervised
choose-cut - convenes decide-and-announce: <who> picks one map
and says why. The maps not chosen are kept with
the reason each one lost
owner: <your sales leadership role>
after: cost-cuts automation: never
fix-map - human: the chosen map at a version, with the rule
it was drawn on and the date it takes effect
owner: territory-modeler after: choose-cut
by: <days> automation: assisted
record-assumptions - human: the assumption under every carrying
number, and what would make it wrong
owner: territory-modeler after: fix-map
automation: assisted
hand-over-map - human: the map, its rules and its carrying
numbers handed to <your quota process>
owner: quota-setter
after: record-assumptions automation: assisted
run-scoped:
data-quality - runs roll-call owner: analytics
every: <cadence>
from: take-in-accounts until: fix-map
handoffs:
take-in-number -> fix-carve-rule [target-and-segments]: the target,
the segments it covers, and the date the map is due
take-in-number -> take-in-accounts [segment-definitions]: the same
segment definitions, so the account base is pulled on the boundaries
the map will use
fix-carve-rule -> draw-candidates [carve-rule]: what a territory is
cut on, written down, and the account fields that rule reads
take-in-accounts -> clean-accounts [account-base]: every account with
the fields the rule needs, and the version each field was read at
take-in-accounts -> read-coverage [accounts-for-coverage]: the same
account base, so coverage is read against the accounts actually
being carved
clean-accounts -> size-accounts [clean-account-base]: one record per
real account, with every account the rule cannot read listed by name
size-accounts -> draw-candidates [account-sizes]: current spend and
carrying size per account, each figure naming the system it came
from
read-coverage -> draw-candidates [last-period-coverage]: which patches
were worked, which were not, and every account nobody called
draw-candidates -> model-carrying [candidate-maps]: the candidate
maps, each placing every account exactly once
model-carrying -> test-history [carrying-numbers]: what each patch
carries, with the assumption beside every number
test-history -> check-balance [backtest-result]: the modelled numbers
set beside what closed, and every patch the model got wrong by more
than <percent>
check-balance -> cost-cuts [balance-findings]: the balance finding per
patch, with the rule behind every failure
cost-cuts -> choose-cut [cut-costs]: what each map costs to staff and
to work, and what it would displace
choose-cut -> fix-map [chosen-map]: the chosen map, who chose it, and
why
fix-map -> record-assumptions [fixed-map]: the map at a version, with
the rule it was drawn on and the date it takes effect
record-assumptions -> hand-over-map [model-assumptions]: the
assumption under every carrying number, and what would make it wrong
deviations:
clean-accounts -> take-in-accounts [field-the-rule-reads-is-missing]:
accounts are missing a field the rule cuts on, so the account base
is taken in again and what stays unreadable is placed by hand
test-history -> model-carrying [model-fails-history]: the model does
not reproduce what those patches actually closed, so the carrying
model is rebuilt rather than the history adjusted
check-balance -> draw-candidates [patch-nobody-could-work]: a patch
comes out too big, too spread or too rich for one seller, so the
candidates are drawn again
choose-cut -> draw-candidates [no-cut-is-workable]: every map on the
table fails balance or cost, so the candidates are drawn again and
the leaders are told which constraint has to move
bindings:
roster: <who holds each role - agents claiming the abstract agents
above, and named people for the leader who chooses the map,
the finance partner and the data steward>
systems: <your account record> (read), the CRM (read),
the territory map (write), the model store (write),
the reporting warehouse (read), the coverage record (write)
data: the target for the period, <your segment definitions> at a
version, the account base at a version, the last <periods>
of closed revenue, <your cost model>
policy:
every account sits in exactly one territory. An account in none is a
defect, and the map does not publish with one
what a territory is cut on is decided before any map is drawn, and a
map drawn on a different rule is redrawn rather than argued about
a carrying number that cannot name the data it came from is marked
unsourced, and the map carries that mark where a reader sees it
the map is chosen by a named person. It is never the average of the
candidates and never the one the model scored highest
a patch nobody has the capacity to work is named as a gap. Raising
its number instead is the failure this process exists to prevent
the maps that were not chosen are kept, each with the reason it lost
measures:
cycle time: <weeks> from the number to the fixed map
coverage: <target> share of accounts placed in a workable patch
accuracy: each patch's modelled carrying number against what it closed
quality gate: no map is fixed while an account is unplaced, in two
patches, or carrying an unsourced number
Copy
Take it somewhere
Use this process in LangGraph
close
Paste this into an assistant that can read the web, such as Claude, ChatGPT or Cursor. It reads the specification and the current LangGraph documentation, then writes two files: the graph, and a note on what did not survive the translation. Read the note first. What a runtime cannot express is the part worth arguing about, and this process is a draft to argue with.
copy the prompt
293 lines · the document is inside it, so nothing else is needed
Convert the business process below into a runnable LangGraph graph:
one Python file with a TypedDict state, a StateGraph, nodes, edges,
conditional edges and a checkpointer.
The document is a reference process written to the Agent Processes
specification. Read the specification before you start, because it defines
terms that look ordinary and are not:
https://agentcatalog.com/spec/agent-processes
Sections 6 (the phase graph), 6.5.1 (exception edges), 6.7.1 (deviations),
7 (automation) and 8 (handoffs) are the ones this conversion turns on.
Then read the current documentation for the primitives you will need, rather
than relying on what you remember of the API:
https://docs.langchain.com/oss/python/langgraph/interrupts
interrupt() and Command(resume=), which is how a gate stops a run
https://reference.langchain.com/python/langgraph/graph/state/StateGraph
StateGraph, add_edge, add_conditional_edges, defer
WHAT THE DOCUMENT ASKS FOR
These hold wherever the process lands, and they matter more than style.
1. Each phase under `phases:` becomes one step, and keeps its name.
2. `after:` gives the edges. `after: a + b` is a join and waits for BOTH.
Reading it as "either" is the defect the specification calls out by name.
3. Every handoff carries a key in square brackets. Each key becomes one field
on the run's state, named exactly as the key with hyphens turned into
underscores, and the sentence beside it becomes that field's comment. The key
is the stable name; the sentence is prose that may be rewritten.
4. A phase MUST NOT begin before its inbound handoff exists. Where that is
checkable, check it in the step rather than assuming it.
5. `automation: never` is a gate a person signs. The run stops there and does
not continue until a person's decision comes back. Do not turn one into a
notification, a log line, or an automatic transition, whatever the queue
looks like.
6. Each line under `deviations:` is a backward or sideways edge, returning to
the phase named on the right. The key in brackets names it, and that name
belongs in the code.
7. A phase whose `after:` reads like "X or Y, whichever could not finish" is an
exception edge: it is entered when those phases FAIL, not when they succeed.
Do not wire it as an ordinary successor.
8. Anything in angle brackets is a blank the adopting organization fills in.
Leave each one as a named constant at the top of the file with a TODO. Do not
invent a value, a threshold or a date.
9. Record the document's `from:` line at the top of the file, so it says which
reference process and which version it was generated from.
10. Run-scoped lines under `run-scoped:` are work that runs alongside the whole
process rather than at one point in it, and a run may not close while one is
unfinished. Say in the code what you did about them, including if the answer
is that the runtime has nowhere to put them.
HOW THAT LOOKS IN LANGGRAPH
11. A phase is a node added with `add_node`, under the phase's own name.
12. The state is a TypedDict. Each handoff key is one field on it.
13. A join is the trap. `add_edge(["a", "b"], "c")` looks right and releases
once: when a backward edge re-enters ONE arm, the joined node never runs
again, and the run ends early reporting success rather than raising. Mark the
joined node `defer=True` and re-check inside it that both inbound handoffs
exist.
14. A gate is `interrupt()` inside the node, resumed with `Command(resume=...)`.
The platform lets anything at all call resume, so require the resumed value to
name a person and a date and refuse anything else. Say in the fidelity note
that this proves only that whoever resumed typed a name, because
`Command(resume=True)` from a scheduled job is indistinguishable from a person
signing.
15. A deviation is `add_conditional_edges` with a routing function named after
the key in brackets.
16. Pass a durable checkpointer rather than taking the in-memory default. The
gates wait days, and the default loses every paused run on restart.
17. Leave every phase body unimplemented, raising until somebody registers an
implementation. The automation level is a blank, so writing a body would
answer on the adopter's behalf whether an agent may do that work.
Produce a second file alongside it, `FIDELITY.md`, and treat it as the more
important of the two. The code is for whoever builds this. The fidelity note
is for whoever has to decide whether this platform suits the process at all,
and that is usually a different person who will never read the code.
It has three parts.
**What came across.** Briefly: how many phases became steps, how many handoff
keys became state fields, which gates stop the run, which deviations became
edges. Counts and names, not reassurance.
**What did not, and what was done instead.** One entry per gap. For each one,
say what the document requires, what the platform can actually express, what
you did in its place, and what breaks if somebody later removes your
workaround. This last part matters most: a workaround nobody understands is a
workaround somebody deletes.
**What a person still has to decide.** The blanks are not a translation
failure, they are the point of a reference process, so list what has to be
filled in before this could run against anything real, and say which of those
choices the platform constrains.
Write it in plain English for somebody who has not read the specification, and
do not soften it. A translation of a reference process is a draft to argue
with, not a build artifact, and the honest account of what was lost is the most
useful thing you will produce.
Here is the process document.
```
PROCESS: model the territories id: <team>/model-the-territories v1
from: ref/rev/model-the-territories v1
owner: <who> effective: <date>
trigger: the carve for <period> opens on <your planning calendar>
watch: record=<the carve for the period>
system=<your planning calendar>
change=<the carve opens for the period>
concurrency: one run per period per market - a re-cut inside the period
is a new version of the same run, not a new run
goal: every account in exactly one territory, every territory modelled
for what it can carry, and one map fixed at a version before any
quota is set against it
phases:
take-in-number - human: the target for the period, the segments it
covers, and the date the map has to be fixed
owner: territory-modeler after: trigger
automation: assisted
fix-carve-rule - convenes decide-and-announce: what a territory is
cut on, whether that is geography, industry, size
or named accounts, decided once and written down
before anything is drawn
owner: <your revenue operations role>
after: take-in-number automation: never
take-in-accounts - runs collect-and-report: every account the carve
will place, with the fields the rule reads,
pulled at one version of <your account record>
owner: analytics after: take-in-number
by: <days> automation: autonomous
clean-accounts - human: duplicates merged into one account, and
every account whose fields the rule cannot read
listed by name rather than filled in by guess
owner: <your data steward role>
after: take-in-accounts
by: <days> automation: assisted
size-accounts - runs assessment: what each account buys today and
what it could carry, each figure naming the
system it was read from
owner: account-monitor after: clean-accounts
by: <days> automation: supervised
read-coverage - runs collect-and-report: which patches were
worked last period, which were never called, and
what was left with no owner at all
owner: coverage-check
after: take-in-accounts automation: autonomous
draw-candidates - runs build-by-talent: <how many> maps drawn on
the agreed rule, each one placing every account
exactly once
owner: territory-modeler
after: fix-carve-rule + size-accounts +
read-coverage
by: <days> automation: supervised
model-carrying - runs build-by-talent: what each patch on each map
can produce in the period, with the assumption
written beside every number
owner: territory-modeler after: draw-candidates
by: <days> automation: supervised
test-history - runs assessment: the model run backwards over the
last <periods> and set beside what those patches
actually closed
owner: statistician after: model-carrying
automation: supervised
check-balance - runs assessment: no patch above <accounts>
accounts or above <amount>, none below <amount>,
and no account sitting in two patches
owner: coverage-check after: test-history
automation: autonomous
cost-cuts - runs allocate-and-reconcile: the sellers, the
travel and the support each map needs, priced
against <your cost model>
owner: capacity-manager after: check-balance
by: <days> automation: supervised
choose-cut - convenes decide-and-announce: <who> picks one map
and says why. The maps not chosen are kept with
the reason each one lost
owner: <your sales leadership role>
after: cost-cuts automation: never
fix-map - human: the chosen map at a version, with the rule
it was drawn on and the date it takes effect
owner: territory-modeler after: choose-cut
by: <days> automation: assisted
record-assumptions - human: the assumption under every carrying
number, and what would make it wrong
owner: territory-modeler after: fix-map
automation: assisted
hand-over-map - human: the map, its rules and its carrying
numbers handed to <your quota process>
owner: quota-setter
after: record-assumptions automation: assisted
run-scoped:
data-quality - runs roll-call owner: analytics
every: <cadence>
from: take-in-accounts until: fix-map
handoffs:
take-in-number -> fix-carve-rule [target-and-segments]: the target,
the segments it covers, and the date the map is due
take-in-number -> take-in-accounts [segment-definitions]: the same
segment definitions, so the account base is pulled on the boundaries
the map will use
fix-carve-rule -> draw-candidates [carve-rule]: what a territory is
cut on, written down, and the account fields that rule reads
take-in-accounts -> clean-accounts [account-base]: every account with
the fields the rule needs, and the version each field was read at
take-in-accounts -> read-coverage [accounts-for-coverage]: the same
account base, so coverage is read against the accounts actually
being carved
clean-accounts -> size-accounts [clean-account-base]: one record per
real account, with every account the rule cannot read listed by name
size-accounts -> draw-candidates [account-sizes]: current spend and
carrying size per account, each figure naming the system it came
from
read-coverage -> draw-candidates [last-period-coverage]: which patches
were worked, which were not, and every account nobody called
draw-candidates -> model-carrying [candidate-maps]: the candidate
maps, each placing every account exactly once
model-carrying -> test-history [carrying-numbers]: what each patch
carries, with the assumption beside every number
test-history -> check-balance [backtest-result]: the modelled numbers
set beside what closed, and every patch the model got wrong by more
than <percent>
check-balance -> cost-cuts [balance-findings]: the balance finding per
patch, with the rule behind every failure
cost-cuts -> choose-cut [cut-costs]: what each map costs to staff and
to work, and what it would displace
choose-cut -> fix-map [chosen-map]: the chosen map, who chose it, and
why
fix-map -> record-assumptions [fixed-map]: the map at a version, with
the rule it was drawn on and the date it takes effect
record-assumptions -> hand-over-map [model-assumptions]: the
assumption under every carrying number, and what would make it wrong
deviations:
clean-accounts -> take-in-accounts [field-the-rule-reads-is-missing]:
accounts are missing a field the rule cuts on, so the account base
is taken in again and what stays unreadable is placed by hand
test-history -> model-carrying [model-fails-history]: the model does
not reproduce what those patches actually closed, so the carrying
model is rebuilt rather than the history adjusted
check-balance -> draw-candidates [patch-nobody-could-work]: a patch
comes out too big, too spread or too rich for one seller, so the
candidates are drawn again
choose-cut -> draw-candidates [no-cut-is-workable]: every map on the
table fails balance or cost, so the candidates are drawn again and
the leaders are told which constraint has to move
bindings:
roster: <who holds each role - agents claiming the abstract agents
above, and named people for the leader who chooses the map,
the finance partner and the data steward>
systems: <your account record> (read), the CRM (read),
the territory map (write), the model store (write),
the reporting warehouse (read), the coverage record (write)
data: the target for the period, <your segment definitions> at a
version, the account base at a version, the last <periods>
of closed revenue, <your cost model>
policy:
every account sits in exactly one territory. An account in none is a
defect, and the map does not publish with one
what a territory is cut on is decided before any map is drawn, and a
map drawn on a different rule is redrawn rather than argued about
a carrying number that cannot name the data it came from is marked
unsourced, and the map carries that mark where a reader sees it
the map is chosen by a named person. It is never the average of the
candidates and never the one the model scored highest
a patch nobody has the capacity to work is named as a gap. Raising
its number instead is the failure this process exists to prevent
the maps that were not chosen are kept, each with the reason it lost
measures:
cycle time: <weeks> from the number to the fixed map
coverage: <target> share of accounts placed in a workable patch
accuracy: each patch's modelled carrying number against what it closed
quality gate: no map is fixed while an account is unplaced, in two
patches, or carrying an unsourced number
```
Take it somewhere
Use this process in Microsoft Agent Framework
close
Paste this into an assistant that can read the web, such as Claude, ChatGPT or Cursor. It reads the specification and the current Agent Framework documentation, then writes two files: the workflow, and a note on what did not survive the translation. Read the note first. What a runtime cannot express is the part worth arguing about, and this process is a draft to argue with.
copy the prompt
348 lines · the document is inside it, so nothing else is needed
Convert the business process below into a runnable Microsoft Agent Framework workflow:
one Python file with Executor classes, a WorkflowBuilder, typed edges,
request_info gates and durable checkpoint storage.
The document is a reference process written to the Agent Processes
specification. Read the specification before you start, because it defines
terms that look ordinary and are not:
https://agentcatalog.com/spec/agent-processes
Sections 6 (the phase graph), 6.5.1 (exception edges), 6.7.1 (deviations),
7 (automation) and 8 (handoffs) are the ones this conversion turns on.
Then read the current documentation for the primitives you will need, rather
than relying on what you remember of the API:
https://learn.microsoft.com/en-us/agent-framework/workflows/human-in-the-loop
ctx.request_info, @response_handler, and answering a parked run later
https://learn.microsoft.com/en-us/agent-framework/workflows/checkpoints
what a checkpoint holds, and allowed_checkpoint_types
https://learn.microsoft.com/en-us/agent-framework/concepts/workflows/edges
add_edge with condition, add_fan_in_edges, add_switch_case_edge_group
https://learn.microsoft.com/en-us/agent-framework/concepts/workflows/state
ctx.set_state and ctx.get_state as they actually are today
WHAT THE DOCUMENT ASKS FOR
These hold wherever the process lands, and they matter more than style.
1. Each phase under `phases:` becomes one step, and keeps its name.
2. `after:` gives the edges. `after: a + b` is a join and waits for BOTH.
Reading it as "either" is the defect the specification calls out by name.
3. Every handoff carries a key in square brackets. Each key becomes one field
on the run's state, named exactly as the key with hyphens turned into
underscores, and the sentence beside it becomes that field's comment. The key
is the stable name; the sentence is prose that may be rewritten.
4. A phase MUST NOT begin before its inbound handoff exists. Where that is
checkable, check it in the step rather than assuming it.
5. `automation: never` is a gate a person signs. The run stops there and does
not continue until a person's decision comes back. Do not turn one into a
notification, a log line, or an automatic transition, whatever the queue
looks like.
6. Each line under `deviations:` is a backward or sideways edge, returning to
the phase named on the right. The key in brackets names it, and that name
belongs in the code.
7. A phase whose `after:` reads like "X or Y, whichever could not finish" is an
exception edge: it is entered when those phases FAIL, not when they succeed.
Do not wire it as an ordinary successor.
8. Anything in angle brackets is a blank the adopting organization fills in.
Leave each one as a named constant at the top of the file with a TODO. Do not
invent a value, a threshold or a date.
9. Record the document's `from:` line at the top of the file, so it says which
reference process and which version it was generated from.
10. Run-scoped lines under `run-scoped:` are work that runs alongside the whole
process rather than at one point in it, and a run may not close while one is
unfinished. Say in the code what you did about them, including if the answer
is that the runtime has nowhere to put them.
HOW THAT LOOKS IN MICROSOFT AGENT FRAMEWORK
11. Write for Python, and read those pages before writing a line. This API has
moved: `set_shared_state`, `RequestInfoExecutor`, `RequestInfoMessage` and
`send_responses_streaming` are all in the training data and none of them exist
any more. The .NET workflow API differs in kind rather than in spelling, so a
file written for one does not port by renaming.
12. A phase is a class deriving from `Executor` whose `super().__init__(id=...)`
takes the phase name verbatim. The id is not cosmetic: a checkpoint stores a
signature over the topology and the executor ids, so an id built from a run, a
timestamp or a counter cannot be resumed into.
13. Give each phase a `@handler` method and move work on with
`await ctx.send_message(...)`. A handler that returns without sending is a dead
end: the branch stops, the run converges, and it reports success. So a phase
you are deliberately leaving unimplemented must still send a placeholder
onward. Do not stub with `raise NotImplementedError`, which fails the run
instead of leaving it runnable.
14. `after: a` is `builder.add_edge(a, b)`. `after: a + b` is
`builder.add_fan_in_edges([a, b], target)`, and the target's handler must be
annotated `list[T]`, because a fan-in delivers one aggregated list rather than
the separate messages. A handler typed for the single value is dropped as a
mismatch with nothing raised.
15. The join is the trap here, and it is the opposite of LangGraph's. The
barrier re-arms: it clears its buffer when it fires and then demands a fresh
message from every source. So a deviation that re-enters ONE arm parks the
run forever waiting for an arm that will not run again, and the workflow ends
IDLE reporting success. Wherever a deviation re-enters one arm of a join,
replace the barrier with an ordinary edge from each arm into a small executor
that records each arrival with `ctx.set_state` and only forwards when every
expected key is present, and say in a comment that putting `add_fan_in_edges`
back reintroduces the stall.
16. A phase that is both a join target and a deviation target needs two
handlers, one annotated `list[T]` for the barrier and one annotated `T` for the
backward message. Write only the list handler and every backward edge into it
is discarded as a type mismatch, silently.
17. `automation: never` is `await ctx.request_info(request_data=...,
response_type=...)` inside the phase, answered by a `@response_handler` on the
same executor whose annotations match those exact types. The run parks at
`IDLE_WITH_PENDING_REQUESTS` and the host answers with
`workflow.run(stream=True, responses={request_id: value})`. If no handler
matches the pair, the framework logs a warning and parks anyway, so the gate
reads as working right up until somebody asks why the approval did not take.
18. A gate is only a gate if the wait survives a restart, so pass
`checkpoint_storage=FileCheckpointStorage(...)` to the builder. Checkpointing
is off by default and `InMemoryCheckpointStorage` reads as configured while
persisting nothing. Register every handoff payload type in
`allowed_checkpoint_types`, or the first restore raises. Anything an executor
keeps as an instance attribute is absent after a restore unless you export it
from `on_checkpoint_save` and read it back in `on_checkpoint_restore`, and it
comes back empty rather than missing.
19. Each line under `deviations:` is `builder.add_edge(source, earlier,
condition=fn)` with `fn` named after the key. Cycles are legal and unchecked,
but raise `max_iterations` well above its default of 100, because several live
cycles will exhaust a budget sized for a straight line and fail with a message
about convergence that reads like a broken graph. Keep the ordinary forward
edge unconditional and add each deviation beside it: a condition that returns
false is dropped with no event, so a forward path expressed as a condition
dies silently on every normal run, which is most of them.
20. Handoff values go in `ctx.set_state(key, value)` and come back from
`ctx.get_state(key)`, untyped and unchecked. A write is visible to its writer
at once and to everyone else only in the next superstep, and two writers of one
key in a superstep keep the last write. Never read a key in the same superstep
another phase wrote it.
21. There are no timers, no deadlines and no scheduled wakes. Nothing in
`run-scoped:` becomes an executor and `by:` has no expression at all, so write
them as comments naming where they start and stop, and say plainly in the
fidelity note that a run can close over an unfinished run-scoped line. Do not
fake a deadline with a sleep inside a handler, which blocks the whole superstep
barrier, and never let an expiring wait release a gate.
Produce a second file alongside it, `FIDELITY.md`, and treat it as the more
important of the two. The code is for whoever builds this. The fidelity note
is for whoever has to decide whether this platform suits the process at all,
and that is usually a different person who will never read the code.
It has three parts.
**What came across.** Briefly: how many phases became steps, how many handoff
keys became state fields, which gates stop the run, which deviations became
edges. Counts and names, not reassurance.
**What did not, and what was done instead.** One entry per gap. For each one,
say what the document requires, what the platform can actually express, what
you did in its place, and what breaks if somebody later removes your
workaround. This last part matters most: a workaround nobody understands is a
workaround somebody deletes.
**What a person still has to decide.** The blanks are not a translation
failure, they are the point of a reference process, so list what has to be
filled in before this could run against anything real, and say which of those
choices the platform constrains.
Write it in plain English for somebody who has not read the specification, and
do not soften it. A translation of a reference process is a draft to argue
with, not a build artifact, and the honest account of what was lost is the most
useful thing you will produce.
Here is the process document.
```
PROCESS: model the territories id: <team>/model-the-territories v1
from: ref/rev/model-the-territories v1
owner: <who> effective: <date>
trigger: the carve for <period> opens on <your planning calendar>
watch: record=<the carve for the period>
system=<your planning calendar>
change=<the carve opens for the period>
concurrency: one run per period per market - a re-cut inside the period
is a new version of the same run, not a new run
goal: every account in exactly one territory, every territory modelled
for what it can carry, and one map fixed at a version before any
quota is set against it
phases:
take-in-number - human: the target for the period, the segments it
covers, and the date the map has to be fixed
owner: territory-modeler after: trigger
automation: assisted
fix-carve-rule - convenes decide-and-announce: what a territory is
cut on, whether that is geography, industry, size
or named accounts, decided once and written down
before anything is drawn
owner: <your revenue operations role>
after: take-in-number automation: never
take-in-accounts - runs collect-and-report: every account the carve
will place, with the fields the rule reads,
pulled at one version of <your account record>
owner: analytics after: take-in-number
by: <days> automation: autonomous
clean-accounts - human: duplicates merged into one account, and
every account whose fields the rule cannot read
listed by name rather than filled in by guess
owner: <your data steward role>
after: take-in-accounts
by: <days> automation: assisted
size-accounts - runs assessment: what each account buys today and
what it could carry, each figure naming the
system it was read from
owner: account-monitor after: clean-accounts
by: <days> automation: supervised
read-coverage - runs collect-and-report: which patches were
worked last period, which were never called, and
what was left with no owner at all
owner: coverage-check
after: take-in-accounts automation: autonomous
draw-candidates - runs build-by-talent: <how many> maps drawn on
the agreed rule, each one placing every account
exactly once
owner: territory-modeler
after: fix-carve-rule + size-accounts +
read-coverage
by: <days> automation: supervised
model-carrying - runs build-by-talent: what each patch on each map
can produce in the period, with the assumption
written beside every number
owner: territory-modeler after: draw-candidates
by: <days> automation: supervised
test-history - runs assessment: the model run backwards over the
last <periods> and set beside what those patches
actually closed
owner: statistician after: model-carrying
automation: supervised
check-balance - runs assessment: no patch above <accounts>
accounts or above <amount>, none below <amount>,
and no account sitting in two patches
owner: coverage-check after: test-history
automation: autonomous
cost-cuts - runs allocate-and-reconcile: the sellers, the
travel and the support each map needs, priced
against <your cost model>
owner: capacity-manager after: check-balance
by: <days> automation: supervised
choose-cut - convenes decide-and-announce: <who> picks one map
and says why. The maps not chosen are kept with
the reason each one lost
owner: <your sales leadership role>
after: cost-cuts automation: never
fix-map - human: the chosen map at a version, with the rule
it was drawn on and the date it takes effect
owner: territory-modeler after: choose-cut
by: <days> automation: assisted
record-assumptions - human: the assumption under every carrying
number, and what would make it wrong
owner: territory-modeler after: fix-map
automation: assisted
hand-over-map - human: the map, its rules and its carrying
numbers handed to <your quota process>
owner: quota-setter
after: record-assumptions automation: assisted
run-scoped:
data-quality - runs roll-call owner: analytics
every: <cadence>
from: take-in-accounts until: fix-map
handoffs:
take-in-number -> fix-carve-rule [target-and-segments]: the target,
the segments it covers, and the date the map is due
take-in-number -> take-in-accounts [segment-definitions]: the same
segment definitions, so the account base is pulled on the boundaries
the map will use
fix-carve-rule -> draw-candidates [carve-rule]: what a territory is
cut on, written down, and the account fields that rule reads
take-in-accounts -> clean-accounts [account-base]: every account with
the fields the rule needs, and the version each field was read at
take-in-accounts -> read-coverage [accounts-for-coverage]: the same
account base, so coverage is read against the accounts actually
being carved
clean-accounts -> size-accounts [clean-account-base]: one record per
real account, with every account the rule cannot read listed by name
size-accounts -> draw-candidates [account-sizes]: current spend and
carrying size per account, each figure naming the system it came
from
read-coverage -> draw-candidates [last-period-coverage]: which patches
were worked, which were not, and every account nobody called
draw-candidates -> model-carrying [candidate-maps]: the candidate
maps, each placing every account exactly once
model-carrying -> test-history [carrying-numbers]: what each patch
carries, with the assumption beside every number
test-history -> check-balance [backtest-result]: the modelled numbers
set beside what closed, and every patch the model got wrong by more
than <percent>
check-balance -> cost-cuts [balance-findings]: the balance finding per
patch, with the rule behind every failure
cost-cuts -> choose-cut [cut-costs]: what each map costs to staff and
to work, and what it would displace
choose-cut -> fix-map [chosen-map]: the chosen map, who chose it, and
why
fix-map -> record-assumptions [fixed-map]: the map at a version, with
the rule it was drawn on and the date it takes effect
record-assumptions -> hand-over-map [model-assumptions]: the
assumption under every carrying number, and what would make it wrong
deviations:
clean-accounts -> take-in-accounts [field-the-rule-reads-is-missing]:
accounts are missing a field the rule cuts on, so the account base
is taken in again and what stays unreadable is placed by hand
test-history -> model-carrying [model-fails-history]: the model does
not reproduce what those patches actually closed, so the carrying
model is rebuilt rather than the history adjusted
check-balance -> draw-candidates [patch-nobody-could-work]: a patch
comes out too big, too spread or too rich for one seller, so the
candidates are drawn again
choose-cut -> draw-candidates [no-cut-is-workable]: every map on the
table fails balance or cost, so the candidates are drawn again and
the leaders are told which constraint has to move
bindings:
roster: <who holds each role - agents claiming the abstract agents
above, and named people for the leader who chooses the map,
the finance partner and the data steward>
systems: <your account record> (read), the CRM (read),
the territory map (write), the model store (write),
the reporting warehouse (read), the coverage record (write)
data: the target for the period, <your segment definitions> at a
version, the account base at a version, the last <periods>
of closed revenue, <your cost model>
policy:
every account sits in exactly one territory. An account in none is a
defect, and the map does not publish with one
what a territory is cut on is decided before any map is drawn, and a
map drawn on a different rule is redrawn rather than argued about
a carrying number that cannot name the data it came from is marked
unsourced, and the map carries that mark where a reader sees it
the map is chosen by a named person. It is never the average of the
candidates and never the one the model scored highest
a patch nobody has the capacity to work is named as a gap. Raising
its number instead is the failure this process exists to prevent
the maps that were not chosen are kept, each with the reason it lost
measures:
cycle time: <weeks> from the number to the fixed map
coverage: <target> share of accounts placed in a workable patch
accuracy: each patch's modelled carrying number against what it closed
quality gate: no map is fixed while an account is unplaced, in two
patches, or carrying an unsourced number
```
Take it somewhere
Use this process in CrewAI Flows
close
Paste this into an assistant that can read the web, such as Claude, ChatGPT or Cursor. It reads the specification and the current CrewAI documentation, then writes two files: the flow, and a note on what did not survive the translation. Read the note first. What a runtime cannot express is the part worth arguing about, and this process is a draft to argue with.
copy the prompt
353 lines · the document is inside it, so nothing else is needed
Convert the business process below into a runnable CrewAI Flow:
one Python file with a Pydantic state model, one Flow subclass, @start,
@listen, @router and a durable human feedback provider.
The document is a reference process written to the Agent Processes
specification. Read the specification before you start, because it defines
terms that look ordinary and are not:
https://agentcatalog.com/spec/agent-processes
Sections 6 (the phase graph), 6.5.1 (exception edges), 6.7.1 (deviations),
7 (automation) and 8 (handoffs) are the ones this conversion turns on.
Then read the current documentation for the primitives you will need, rather
than relying on what you remember of the API:
https://docs.crewai.com/en/concepts/flows
Flow, @start, @listen, @router, and_, or_, state, kickoff, plot
https://docs.crewai.com/en/learn/human-feedback-in-flows
@human_feedback, the provider protocol, from_pending and resume
https://docs.crewai.com/en/guides/flows/mastering-flow-state
@persist and what persistence actually promises, which is less than it sounds
WHAT THE DOCUMENT ASKS FOR
These hold wherever the process lands, and they matter more than style.
1. Each phase under `phases:` becomes one step, and keeps its name.
2. `after:` gives the edges. `after: a + b` is a join and waits for BOTH.
Reading it as "either" is the defect the specification calls out by name.
3. Every handoff carries a key in square brackets. Each key becomes one field
on the run's state, named exactly as the key with hyphens turned into
underscores, and the sentence beside it becomes that field's comment. The key
is the stable name; the sentence is prose that may be rewritten.
4. A phase MUST NOT begin before its inbound handoff exists. Where that is
checkable, check it in the step rather than assuming it.
5. `automation: never` is a gate a person signs. The run stops there and does
not continue until a person's decision comes back. Do not turn one into a
notification, a log line, or an automatic transition, whatever the queue
looks like.
6. Each line under `deviations:` is a backward or sideways edge, returning to
the phase named on the right. The key in brackets names it, and that name
belongs in the code.
7. A phase whose `after:` reads like "X or Y, whichever could not finish" is an
exception edge: it is entered when those phases FAIL, not when they succeed.
Do not wire it as an ordinary successor.
8. Anything in angle brackets is a blank the adopting organization fills in.
Leave each one as a named constant at the top of the file with a TODO. Do not
invent a value, a threshold or a date.
9. Record the document's `from:` line at the top of the file, so it says which
reference process and which version it was generated from.
10. Run-scoped lines under `run-scoped:` are work that runs alongside the whole
process rather than at one point in it, and a run may not close while one is
unfinished. Say in the code what you did about them, including if the answer
is that the runtime has nowhere to put them.
HOW THAT LOOKS IN CREWAI FLOWS
11. Build a Flow, not a Crew. A Crew is a team of roles with no graph, no join,
no persistence handle and no gate, and cannot express this document at all.
Each phase is one method on a single `Flow` subclass, keeping its name with
hyphens turned into underscores. A phase that genuinely needs role-based agents
builds its own Crew inside its own method body, and the Flow stays the graph.
12. Declare the state as a Pydantic model bound as the type parameter,
`class NegotiateTheAgreement(Flow[NegotiationState])`, and read and write
`self.state.field`. Never use the untyped dict form: the handoff keys are this
process's memory, and an untyped dict turns a misspelt key into a handoff that
is silently absent. Keep the auto-injected `id` field, which is what every
resume depends on.
13. `after: a` is `@listen(a)`. `after: a + b` is `@listen(and_(a, b))`. Never
write `or_` where the document writes `+`.
14. Check every inbound handoff at the top of the method and raise if one is
missing. `@listen` says when a method may run and says nothing about what is
in hand when it does.
15. The join is the trap, and it is proven rather than theoretical. `and_()`
empties its accumulator the moment it fires, so re-entering BOTH arms works
forever, and re-entering ONE arm after it has fired leaves it holding a single
trigger and waiting for the other for good. The cascade drains, CrewAI prints
that the flow completed, and `kickoff()` returns normally. So for any joining
phase that a deviation can send work back into, do not use `and_()` at all:
make the join a `@router` that both arms trigger, which reads the state fields
and emits its label only when every inbound handoff is present. A router is
re-evaluated against durable state every time and is never suppressed by the
once-fired set.
16. Do not write a phase as `@listen(or_(and_(a, b), "some_label"))`. There is
one accumulator per listener, shared across every branch of its condition and
wiped when any branch satisfies, so the label firing while the join is half
full erases the arm that had already arrived.
17. `automation: never` is `@human_feedback(message=..., provider=...)` stacked
under the method's `@listen`, with a provider whose `request_feedback` raises
`HumanFeedbackPending`. The run then persists, returns that object from
`kickoff()`, and a different process answers later with `from_pending(flow_id,
persistence)` and `resume(text)`. Do not take the default `ConsoleProvider`,
which calls `input()`: that gate exists only while somebody is watching a
terminal, and a run started by a scheduler either hangs or takes an empty
string.
18. Silence must not approve, and the platform's default is that it does. With
`emit=[...]` set, an empty resume collapses to `default_outcome`, or to the
first label when that is unset, with no model consulted and nobody named. Treat
an empty or unrecognised answer as a refusal in your own router. And write the
approver's name and the time onto the state yourself, because
`HumanFeedbackResult` carries the text, the outcome and a timestamp but has no
field for the person, which the specification requires.
19. Each line under `deviations:` is a `@router` named after the key, returning
a label named after the same key, with the target subscribing as
`@listen(or_(normal_trigger, "the_label"))`. Route rather than listen
directly, because a router is re-evaluated on every cycle while a top-level
`or_` listener is suppressed after it first fires.
20. An exception edge is a `try` and `except` around the failing phase's body,
recording the failure on the state and emitting an exception label from a
router. Wiring it as `@listen(or_(x, y))` fires when those phases SUCCEED, so
the clearing phase would run on every healthy run.
21. An unimplemented phase is a method with the document's own sentence as its
docstring and a bare `pass`, which is safe because listeners still fire on a
`None` return. An unimplemented `@router` is not safe: returning `None` emits
no label, every phase below it disappears from the run including the gates, and
the flow reports success. A stub router must return a hard-coded label with a
TODO beside it, or raise.
22. Turn persistence on with `@persist(SQLiteFlowPersistence(...))`, then treat
every phase that performs a real act as something that will run twice.
Persistence saves the state fields and nothing else, so a restart rehydrates
the data and runs the graph again from `@start`: a flow that had already sent a
written refusal sends a second one. Guard each acting phase with a state field
it checks and sets.
23. There are no timers, no deadlines and no cadences, and `run-scoped:` has no
counterpart at all. Put each `by:` value as a named constant, name the
run-scoped lines in the module docstring as unimplemented obligations, and say
in the fidelity note that no deadline in this document is enforced by anything.
These absences produce no diagnostic whatsoever, which is exactly why they have
to be written down.
Produce a second file alongside it, `FIDELITY.md`, and treat it as the more
important of the two. The code is for whoever builds this. The fidelity note
is for whoever has to decide whether this platform suits the process at all,
and that is usually a different person who will never read the code.
It has three parts.
**What came across.** Briefly: how many phases became steps, how many handoff
keys became state fields, which gates stop the run, which deviations became
edges. Counts and names, not reassurance.
**What did not, and what was done instead.** One entry per gap. For each one,
say what the document requires, what the platform can actually express, what
you did in its place, and what breaks if somebody later removes your
workaround. This last part matters most: a workaround nobody understands is a
workaround somebody deletes.
**What a person still has to decide.** The blanks are not a translation
failure, they are the point of a reference process, so list what has to be
filled in before this could run against anything real, and say which of those
choices the platform constrains.
Write it in plain English for somebody who has not read the specification, and
do not soften it. A translation of a reference process is a draft to argue
with, not a build artifact, and the honest account of what was lost is the most
useful thing you will produce.
Here is the process document.
```
PROCESS: model the territories id: <team>/model-the-territories v1
from: ref/rev/model-the-territories v1
owner: <who> effective: <date>
trigger: the carve for <period> opens on <your planning calendar>
watch: record=<the carve for the period>
system=<your planning calendar>
change=<the carve opens for the period>
concurrency: one run per period per market - a re-cut inside the period
is a new version of the same run, not a new run
goal: every account in exactly one territory, every territory modelled
for what it can carry, and one map fixed at a version before any
quota is set against it
phases:
take-in-number - human: the target for the period, the segments it
covers, and the date the map has to be fixed
owner: territory-modeler after: trigger
automation: assisted
fix-carve-rule - convenes decide-and-announce: what a territory is
cut on, whether that is geography, industry, size
or named accounts, decided once and written down
before anything is drawn
owner: <your revenue operations role>
after: take-in-number automation: never
take-in-accounts - runs collect-and-report: every account the carve
will place, with the fields the rule reads,
pulled at one version of <your account record>
owner: analytics after: take-in-number
by: <days> automation: autonomous
clean-accounts - human: duplicates merged into one account, and
every account whose fields the rule cannot read
listed by name rather than filled in by guess
owner: <your data steward role>
after: take-in-accounts
by: <days> automation: assisted
size-accounts - runs assessment: what each account buys today and
what it could carry, each figure naming the
system it was read from
owner: account-monitor after: clean-accounts
by: <days> automation: supervised
read-coverage - runs collect-and-report: which patches were
worked last period, which were never called, and
what was left with no owner at all
owner: coverage-check
after: take-in-accounts automation: autonomous
draw-candidates - runs build-by-talent: <how many> maps drawn on
the agreed rule, each one placing every account
exactly once
owner: territory-modeler
after: fix-carve-rule + size-accounts +
read-coverage
by: <days> automation: supervised
model-carrying - runs build-by-talent: what each patch on each map
can produce in the period, with the assumption
written beside every number
owner: territory-modeler after: draw-candidates
by: <days> automation: supervised
test-history - runs assessment: the model run backwards over the
last <periods> and set beside what those patches
actually closed
owner: statistician after: model-carrying
automation: supervised
check-balance - runs assessment: no patch above <accounts>
accounts or above <amount>, none below <amount>,
and no account sitting in two patches
owner: coverage-check after: test-history
automation: autonomous
cost-cuts - runs allocate-and-reconcile: the sellers, the
travel and the support each map needs, priced
against <your cost model>
owner: capacity-manager after: check-balance
by: <days> automation: supervised
choose-cut - convenes decide-and-announce: <who> picks one map
and says why. The maps not chosen are kept with
the reason each one lost
owner: <your sales leadership role>
after: cost-cuts automation: never
fix-map - human: the chosen map at a version, with the rule
it was drawn on and the date it takes effect
owner: territory-modeler after: choose-cut
by: <days> automation: assisted
record-assumptions - human: the assumption under every carrying
number, and what would make it wrong
owner: territory-modeler after: fix-map
automation: assisted
hand-over-map - human: the map, its rules and its carrying
numbers handed to <your quota process>
owner: quota-setter
after: record-assumptions automation: assisted
run-scoped:
data-quality - runs roll-call owner: analytics
every: <cadence>
from: take-in-accounts until: fix-map
handoffs:
take-in-number -> fix-carve-rule [target-and-segments]: the target,
the segments it covers, and the date the map is due
take-in-number -> take-in-accounts [segment-definitions]: the same
segment definitions, so the account base is pulled on the boundaries
the map will use
fix-carve-rule -> draw-candidates [carve-rule]: what a territory is
cut on, written down, and the account fields that rule reads
take-in-accounts -> clean-accounts [account-base]: every account with
the fields the rule needs, and the version each field was read at
take-in-accounts -> read-coverage [accounts-for-coverage]: the same
account base, so coverage is read against the accounts actually
being carved
clean-accounts -> size-accounts [clean-account-base]: one record per
real account, with every account the rule cannot read listed by name
size-accounts -> draw-candidates [account-sizes]: current spend and
carrying size per account, each figure naming the system it came
from
read-coverage -> draw-candidates [last-period-coverage]: which patches
were worked, which were not, and every account nobody called
draw-candidates -> model-carrying [candidate-maps]: the candidate
maps, each placing every account exactly once
model-carrying -> test-history [carrying-numbers]: what each patch
carries, with the assumption beside every number
test-history -> check-balance [backtest-result]: the modelled numbers
set beside what closed, and every patch the model got wrong by more
than <percent>
check-balance -> cost-cuts [balance-findings]: the balance finding per
patch, with the rule behind every failure
cost-cuts -> choose-cut [cut-costs]: what each map costs to staff and
to work, and what it would displace
choose-cut -> fix-map [chosen-map]: the chosen map, who chose it, and
why
fix-map -> record-assumptions [fixed-map]: the map at a version, with
the rule it was drawn on and the date it takes effect
record-assumptions -> hand-over-map [model-assumptions]: the
assumption under every carrying number, and what would make it wrong
deviations:
clean-accounts -> take-in-accounts [field-the-rule-reads-is-missing]:
accounts are missing a field the rule cuts on, so the account base
is taken in again and what stays unreadable is placed by hand
test-history -> model-carrying [model-fails-history]: the model does
not reproduce what those patches actually closed, so the carrying
model is rebuilt rather than the history adjusted
check-balance -> draw-candidates [patch-nobody-could-work]: a patch
comes out too big, too spread or too rich for one seller, so the
candidates are drawn again
choose-cut -> draw-candidates [no-cut-is-workable]: every map on the
table fails balance or cost, so the candidates are drawn again and
the leaders are told which constraint has to move
bindings:
roster: <who holds each role - agents claiming the abstract agents
above, and named people for the leader who chooses the map,
the finance partner and the data steward>
systems: <your account record> (read), the CRM (read),
the territory map (write), the model store (write),
the reporting warehouse (read), the coverage record (write)
data: the target for the period, <your segment definitions> at a
version, the account base at a version, the last <periods>
of closed revenue, <your cost model>
policy:
every account sits in exactly one territory. An account in none is a
defect, and the map does not publish with one
what a territory is cut on is decided before any map is drawn, and a
map drawn on a different rule is redrawn rather than argued about
a carrying number that cannot name the data it came from is marked
unsourced, and the map carries that mark where a reader sees it
the map is chosen by a named person. It is never the average of the
candidates and never the one the model scored highest
a patch nobody has the capacity to work is named as a gap. Raising
its number instead is the failure this process exists to prevent
the maps that were not chosen are kept, each with the reason it lost
measures:
cycle time: <weeks> from the number to the fixed map
coverage: <target> share of accounts placed in a workable patch
accuracy: each patch's modelled carrying number against what it closed
quality gate: no map is fixed while an account is unplaced, in two
patches, or carrying an unsourced number
```
Take it somewhere
Use this process in Google ADK
close
Paste this into an assistant that can read the web, such as Claude, ChatGPT or Cursor. It reads the specification and the current ADK documentation, then writes two files: the workflow, and a note on what did not survive the translation. Read the note first. What a runtime cannot express is the part worth arguing about, and this process is a draft to argue with.
copy the prompt
344 lines · the document is inside it, so nothing else is needed
Convert the business process below into a runnable Google ADK workflow:
one Python file with a Workflow, nodes, routed edges, a JoinNode,
RequestInput gates and a persisting session service.
The document is a reference process written to the Agent Processes
specification. Read the specification before you start, because it defines
terms that look ordinary and are not:
https://agentcatalog.com/spec/agent-processes
Sections 6 (the phase graph), 6.5.1 (exception edges), 6.7.1 (deviations),
7 (automation) and 8 (handoffs) are the ones this conversion turns on.
Then read the current documentation for the primitives you will need, rather
than relying on what you remember of the API:
https://adk.dev/graphs/routes/
nodes, tuple chains, Event(route=), JoinNode, back-edges
https://adk.dev/graphs/human-input/
RequestInput and the rerun_on_resume handoff
https://adk.dev/runtime/resume/
ResumabilityConfig, resuming by invocation id, at-least-once tools
https://adk.dev/graphs/data-handling/
Event.output against state, and the selector syntax in instructions
WHAT THE DOCUMENT ASKS FOR
These hold wherever the process lands, and they matter more than style.
1. Each phase under `phases:` becomes one step, and keeps its name.
2. `after:` gives the edges. `after: a + b` is a join and waits for BOTH.
Reading it as "either" is the defect the specification calls out by name.
3. Every handoff carries a key in square brackets. Each key becomes one field
on the run's state, named exactly as the key with hyphens turned into
underscores, and the sentence beside it becomes that field's comment. The key
is the stable name; the sentence is prose that may be rewritten.
4. A phase MUST NOT begin before its inbound handoff exists. Where that is
checkable, check it in the step rather than assuming it.
5. `automation: never` is a gate a person signs. The run stops there and does
not continue until a person's decision comes back. Do not turn one into a
notification, a log line, or an automatic transition, whatever the queue
looks like.
6. Each line under `deviations:` is a backward or sideways edge, returning to
the phase named on the right. The key in brackets names it, and that name
belongs in the code.
7. A phase whose `after:` reads like "X or Y, whichever could not finish" is an
exception edge: it is entered when those phases FAIL, not when they succeed.
Do not wire it as an ordinary successor.
8. Anything in angle brackets is a blank the adopting organization fills in.
Leave each one as a named constant at the top of the file with a TODO. Do not
invent a value, a threshold or a date.
9. Record the document's `from:` line at the top of the file, so it says which
reference process and which version it was generated from.
10. Run-scoped lines under `run-scoped:` are work that runs alongside the whole
process rather than at one point in it, and a run may not close while one is
unfinished. Say in the code what you did about them, including if the answer
is that the runtime has nowhere to put them.
HOW THAT LOOKS IN GOOGLE ADK
11. Build a `Workflow` from `google.adk.workflow`, and pin `google-adk>=2.0` in
a comment. Do not use `SequentialAgent`, `ParallelAgent` or `LoopAgent`: they
are deprecated in favour of the graph, and they carry their own defects around
state and control flow. The documentation moved to adk.dev, and anything you
remember about nesting agents rather than drawing a graph is out of date.
12. Each phase is one node keeping its name. Take the node kind from how the
phase resolves rather than from taste: a `human:` or `system:` phase is a plain
Python function node, and a `runs` or `convenes` phase is an `Agent`.
13. `after:` gives the edges, written as tuple chains in `edges=[...]`, and the
trigger is the `"START"` keyword. Take the order only from `after:` lines and
never from the order the phases are listed in.
14. `after: a + b` is a `JoinNode`, and it must be guarded, because this is the
worst trap of any runtime here. The join fires when every static predecessor is
marked COMPLETED, nothing ever un-completes a node, and stored outputs are
never cleared. So after a deviation re-runs one arm, the join fires the instant
that arm finishes and hands the next phase LAST PASS'S value for every arm that
did not re-run. It does not stall, it proceeds with stale data, and nothing
logs. Stamp each arm's output with a pass counter or a content hash, and have
the phase after the join compare the stamps and refuse to run when they
disagree.
15. Each line under `deviations:` is a routed back-edge: a router after the
phase on the left returning `Event(route=...)`, named after the key in
brackets, with one arm going back to the phase on the right and one going
forward. An unconditional cycle raises at construction, which is the one place
this model checks your work. Nothing budgets a routed cycle, so add your own
count and stop rather than looping forever.
16. Give every router an explicit `DEFAULT_ROUTE` arm, and route it to a phase
that stops and asks a person. A route value matching no key writes a log
warning, ends that branch, and lets the run finish reporting success with the
rest of the process never having happened.
17. `automation: never` is a `RequestInput` node of its own, never an `Agent`
asking a question. Decorate it `@node(rerun_on_resume=False)` and yield
`RequestInput(message=..., payload=..., response_schema=...)`, so the run
stops, persists, and delivers the person's answer to the node's successor as
its typed input. A resumed workflow runs its tools at least once, so any
irreversible act needs its own duplicate guard.
18. Make the gates durable or say plainly that they are not. Wrap the graph in
`App(..., resumability_config=ResumabilityConfig(is_resumable=True))` and pass
a persisting session service, never the in-memory one. Note in the file that
the command line and the web UI cannot resume a run, so whoever releases these
gates needs an operator surface that somebody has to write.
19. Every `Agent` in the graph gets `mode="single_turn"` and no `sub_agents`.
A non-empty `sub_agents` list silently adds a transfer tool, and a model that
uses it runs a different agent in this node's place while the graph's outgoing
edge fires on schedule regardless: the topology is honoured perfectly and the
work belongs to somebody else.
20. Model failure as a route, not as an exception. A node that raises does not
propagate: the failure is caught, recorded, and shuts the workflow down without
raising to the caller. So a phase that can fail catches its own failure and
returns `Event(route="could-not-finish")`, and the exception phase hangs off
that arm.
21. Keep every blank as a named module-level constant and never interpolate one
into an `instruction=` string. Angle brackets and curly braces are ADK's own
data selector syntax inside instructions, so a blank pasted verbatim stops
being a blank and becomes a selector.
22. `by:` and `not-before:` have no expression, and `@node(timeout=)` is not
one: it is an in-process wall clock that cancels the node and, because failures
are swallowed, ends the run silently rather than recording a missed deadline.
Nothing in `run-scoped:` has an expression either, and it must not be faked as
an ordinary node, because a node has to be reached and has to finish before
anything downstream starts, which is the opposite of what those lines mean.
Leave both out of the graph and name them in the fidelity note.
Produce a second file alongside it, `FIDELITY.md`, and treat it as the more
important of the two. The code is for whoever builds this. The fidelity note
is for whoever has to decide whether this platform suits the process at all,
and that is usually a different person who will never read the code.
It has three parts.
**What came across.** Briefly: how many phases became steps, how many handoff
keys became state fields, which gates stop the run, which deviations became
edges. Counts and names, not reassurance.
**What did not, and what was done instead.** One entry per gap. For each one,
say what the document requires, what the platform can actually express, what
you did in its place, and what breaks if somebody later removes your
workaround. This last part matters most: a workaround nobody understands is a
workaround somebody deletes.
**What a person still has to decide.** The blanks are not a translation
failure, they are the point of a reference process, so list what has to be
filled in before this could run against anything real, and say which of those
choices the platform constrains.
Write it in plain English for somebody who has not read the specification, and
do not soften it. A translation of a reference process is a draft to argue
with, not a build artifact, and the honest account of what was lost is the most
useful thing you will produce.
Here is the process document.
```
PROCESS: model the territories id: <team>/model-the-territories v1
from: ref/rev/model-the-territories v1
owner: <who> effective: <date>
trigger: the carve for <period> opens on <your planning calendar>
watch: record=<the carve for the period>
system=<your planning calendar>
change=<the carve opens for the period>
concurrency: one run per period per market - a re-cut inside the period
is a new version of the same run, not a new run
goal: every account in exactly one territory, every territory modelled
for what it can carry, and one map fixed at a version before any
quota is set against it
phases:
take-in-number - human: the target for the period, the segments it
covers, and the date the map has to be fixed
owner: territory-modeler after: trigger
automation: assisted
fix-carve-rule - convenes decide-and-announce: what a territory is
cut on, whether that is geography, industry, size
or named accounts, decided once and written down
before anything is drawn
owner: <your revenue operations role>
after: take-in-number automation: never
take-in-accounts - runs collect-and-report: every account the carve
will place, with the fields the rule reads,
pulled at one version of <your account record>
owner: analytics after: take-in-number
by: <days> automation: autonomous
clean-accounts - human: duplicates merged into one account, and
every account whose fields the rule cannot read
listed by name rather than filled in by guess
owner: <your data steward role>
after: take-in-accounts
by: <days> automation: assisted
size-accounts - runs assessment: what each account buys today and
what it could carry, each figure naming the
system it was read from
owner: account-monitor after: clean-accounts
by: <days> automation: supervised
read-coverage - runs collect-and-report: which patches were
worked last period, which were never called, and
what was left with no owner at all
owner: coverage-check
after: take-in-accounts automation: autonomous
draw-candidates - runs build-by-talent: <how many> maps drawn on
the agreed rule, each one placing every account
exactly once
owner: territory-modeler
after: fix-carve-rule + size-accounts +
read-coverage
by: <days> automation: supervised
model-carrying - runs build-by-talent: what each patch on each map
can produce in the period, with the assumption
written beside every number
owner: territory-modeler after: draw-candidates
by: <days> automation: supervised
test-history - runs assessment: the model run backwards over the
last <periods> and set beside what those patches
actually closed
owner: statistician after: model-carrying
automation: supervised
check-balance - runs assessment: no patch above <accounts>
accounts or above <amount>, none below <amount>,
and no account sitting in two patches
owner: coverage-check after: test-history
automation: autonomous
cost-cuts - runs allocate-and-reconcile: the sellers, the
travel and the support each map needs, priced
against <your cost model>
owner: capacity-manager after: check-balance
by: <days> automation: supervised
choose-cut - convenes decide-and-announce: <who> picks one map
and says why. The maps not chosen are kept with
the reason each one lost
owner: <your sales leadership role>
after: cost-cuts automation: never
fix-map - human: the chosen map at a version, with the rule
it was drawn on and the date it takes effect
owner: territory-modeler after: choose-cut
by: <days> automation: assisted
record-assumptions - human: the assumption under every carrying
number, and what would make it wrong
owner: territory-modeler after: fix-map
automation: assisted
hand-over-map - human: the map, its rules and its carrying
numbers handed to <your quota process>
owner: quota-setter
after: record-assumptions automation: assisted
run-scoped:
data-quality - runs roll-call owner: analytics
every: <cadence>
from: take-in-accounts until: fix-map
handoffs:
take-in-number -> fix-carve-rule [target-and-segments]: the target,
the segments it covers, and the date the map is due
take-in-number -> take-in-accounts [segment-definitions]: the same
segment definitions, so the account base is pulled on the boundaries
the map will use
fix-carve-rule -> draw-candidates [carve-rule]: what a territory is
cut on, written down, and the account fields that rule reads
take-in-accounts -> clean-accounts [account-base]: every account with
the fields the rule needs, and the version each field was read at
take-in-accounts -> read-coverage [accounts-for-coverage]: the same
account base, so coverage is read against the accounts actually
being carved
clean-accounts -> size-accounts [clean-account-base]: one record per
real account, with every account the rule cannot read listed by name
size-accounts -> draw-candidates [account-sizes]: current spend and
carrying size per account, each figure naming the system it came
from
read-coverage -> draw-candidates [last-period-coverage]: which patches
were worked, which were not, and every account nobody called
draw-candidates -> model-carrying [candidate-maps]: the candidate
maps, each placing every account exactly once
model-carrying -> test-history [carrying-numbers]: what each patch
carries, with the assumption beside every number
test-history -> check-balance [backtest-result]: the modelled numbers
set beside what closed, and every patch the model got wrong by more
than <percent>
check-balance -> cost-cuts [balance-findings]: the balance finding per
patch, with the rule behind every failure
cost-cuts -> choose-cut [cut-costs]: what each map costs to staff and
to work, and what it would displace
choose-cut -> fix-map [chosen-map]: the chosen map, who chose it, and
why
fix-map -> record-assumptions [fixed-map]: the map at a version, with
the rule it was drawn on and the date it takes effect
record-assumptions -> hand-over-map [model-assumptions]: the
assumption under every carrying number, and what would make it wrong
deviations:
clean-accounts -> take-in-accounts [field-the-rule-reads-is-missing]:
accounts are missing a field the rule cuts on, so the account base
is taken in again and what stays unreadable is placed by hand
test-history -> model-carrying [model-fails-history]: the model does
not reproduce what those patches actually closed, so the carrying
model is rebuilt rather than the history adjusted
check-balance -> draw-candidates [patch-nobody-could-work]: a patch
comes out too big, too spread or too rich for one seller, so the
candidates are drawn again
choose-cut -> draw-candidates [no-cut-is-workable]: every map on the
table fails balance or cost, so the candidates are drawn again and
the leaders are told which constraint has to move
bindings:
roster: <who holds each role - agents claiming the abstract agents
above, and named people for the leader who chooses the map,
the finance partner and the data steward>
systems: <your account record> (read), the CRM (read),
the territory map (write), the model store (write),
the reporting warehouse (read), the coverage record (write)
data: the target for the period, <your segment definitions> at a
version, the account base at a version, the last <periods>
of closed revenue, <your cost model>
policy:
every account sits in exactly one territory. An account in none is a
defect, and the map does not publish with one
what a territory is cut on is decided before any map is drawn, and a
map drawn on a different rule is redrawn rather than argued about
a carrying number that cannot name the data it came from is marked
unsourced, and the map carries that mark where a reader sees it
the map is chosen by a named person. It is never the average of the
candidates and never the one the model scored highest
a patch nobody has the capacity to work is named as a gap. Raising
its number instead is the failure this process exists to prevent
the maps that were not chosen are kept, each with the reason it lost
measures:
cycle time: <weeks> from the number to the fixed map
coverage: <target> share of accounts placed in a workable patch
accuracy: each patch's modelled carrying number against what it closed
quality gate: no map is fixed while an account is unplaced, in two
patches, or carrying an unsourced number
```
One run
A simulation of one run
The activities are on the left, whoever is doing the active one is on the right, and the record of the run builds up as it goes.
This run is built from the same rows as the diagram above: the left column is the activity list, the captions are the activity lines, the cast is the roster, and the labels on the wires are what the handoffs say actually passes.
Adoption
What you fill in
35 blanks to fill. Everything else is the process.
this process
from: ref/rev/model-the-territories v1
Copy this line into your own document. It never claims this process is running anywhere; it records which draft yours started from, and it is what lets the catalog tell you when this one changes.
The header. Your own id, an owner, and the date it takes effect. One line records where it came from, and that line is what lets the catalog tell you when this reference process changes.
The roster. Which agent takes each activity, and which person takes each of the human ones. The process already names what it needs, so this is a lookup rather than a design exercise.
The numbers. Dates, budgets, cadences, and the targets in the measures block. Nothing here can be a reference value, because a target nobody chose is a target nobody meets.