Jangada AIJangada AI

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 chain

Each 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 in GraphResult.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 a str instruction. Trims the join's input (it reads every branch and grows O(N)); the summarization call is counted in usage/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 of Flow.
  • Graph(max_steps=50): cap on nodes executed in a run; a cyclic router that never ends raises RuntimeError instead 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#3 in completions/durations, and usage/cost add 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_servers or self — they clashed with complete's arguments and caused odd errors. They now raise ValueError right away.
  • then()/route() on a parallel block raise an error (they used to be silently ignored); a join pointing to a missing node has its own message.

Example

examples/graph_example.py — runnable script.

On this page