Flows and orchestration (Flow and Graph)
Jangada provides two ways to chain calls, both aggregating usage/cost.
Flow — sequential
Flow chains steps: the output of one becomes a variable of the next.
from jangada_ai import LLM, Flow
llm = LLM("openai", "gpt-4o-mini")
flow = (
Flow(llm) # default LLM for the steps
.step("draft", "Write a paragraph about {{topic}}.")
.step("review", "Review and improve:\n{{draft}}")
)
res = flow.run(topic="jangadas of the Northeast") # async: await flow.arun(...)
print(res["review"]) # a step's output by name
print(res.last) # output of the last step
print(res.cost) # aggregated cost of the whole chainEach step references previous outputs by name via the {{ }} template.
step() also takes system=, llm= (another model for that step only),
output= (variable name, if different from the step) and schema= (uses
parse(); read it with res.parsed("step")).
Graph — conditional routing + parallel
Graph lets you branch (conditional routing) and run nodes in parallel
(async core), joining the results.
from jangada_ai import LLM, Graph
fast = LLM("groq", "openai/gpt-oss-20b")
strong = LLM("anthropic", "claude-sonnet-4-5")
g = Graph()
g.node("triage", fast, "Answer only 'technical' or 'general': {{question}}")
g.node("technical", strong, "Answer technically: {{question}}")
g.node("general", fast, "Answer simply: {{question}}")
# conditional routing: the router gets the context and returns the next node (or None)
g.route("triage", lambda ctx: "technical" if "technical" in ctx["triage"].lower() else "general")
res = g.run("triage", question="how does the TCP handshake work?") # async: await g.arun(...)
print(res.path) # e.g. ['triage', 'technical']
print(res[res.path[-1]]) # output of the last node
print(res.cost) # GraphResult aggregates usage/cost
# parallel + join: branches run at the same time and the join reads each by name
g2 = Graph()
g2.parallel(
"research",
{
"market": (fast, "Analyze the market for: {{topic}}"),
"tech": (strong, "Analyze the technical feasibility of: {{topic}}"),
},
join="synthesis",
)
g2.node("synthesis", strong, "Combine:\nMarket: {{market}}\nTech: {{tech}}")
print(g2.run("research", topic="AI running app")["synthesis"])To chain nodes unconditionally, use g.then("a", "b").
See the runnable examples in examples/graph_example.py.
Robust, observable fan-out (parallel)
parallel takes production-grade options:
g.parallel(
"analyses", branches, join="synthesis",
on_error="skip", # a failing branch won't tear down the run (default: "raise")
max_concurrency=4, # cap on simultaneous branches (avoids rate limits)
summarize=True, # each branch summarizes its own output before the join
)on_error="skip": a failing branch is left out of the join (empty string in the context) and recorded inGraphResult.failures({branch: error}). The fan-out returns the partial result instead of throwing away the already-paid work of the other branches. With"raise"(default) the first exception tears down the run.max_concurrency=N: semaphore — at most N branches run at once.summarize:True(default instruction) or astrinstruction. Trims the join's input (it reads every branch and grows O(N)); the summarization call is counted inusage/cost.
GraphResult also exposes durations ({node/branch: seconds}) — so you can
measure max(branches) vs t_join directly, the diagnostic that matters in a fan-out.
Aggregated cost
FlowResult and GraphResult sum usage and cost across all stages — useful
for observability. Details in Cost and tokens and, to inspect
step by step, Debug.
What changed in 1.9.0
Flow.arun(**context): async version ofFlow.Graph(max_steps=50): cap on nodes executed in arun; a cyclic router that never ends raisesRuntimeErrorinstead of spending without limit.- A revisited node (cycle) or a step with a repeated name no longer
overwrites the previous one: visits show up as
name#2,name#3incompletions/durations, andusage/costadd them all up.parsed("name")returns the last visit. - Parallel branches with
on_error="raise": when a branch fails, the pending sibling branches are cancelled (they used to keep running and spending tokens). - Reserved names. Nodes, steps, branches and context variables can't be
called
system,history,params,tools,tool_choice,files,images,schema,prompt,mcp_serversorself— they clashed withcomplete's arguments and caused odd errors. They now raiseValueErrorright away. then()/route()on a parallel block raise an error (they used to be silently ignored); ajoinpointing to a missing node has its own message.
Example
examples/graph_example.py — runnable script.
Extending: adding a provider
Each provider is an adapter that inherits from Provider and translates the normalized types to the native SDK.
Migrating from LangChain
Coming from LangChain? This guide shows, capability by capability, how each thing is done in jangada — the library's own pattern, not a LangChain clone — and what the differentials are.