Mixing deterministic Python transforms with AI steps

Use Python for math, normalization, and validation — use LLMs for judgment

The fastest, cheapest, most reliable AgentFlows never ask an LLM to do what 5 lines of Python do deterministically. Sandwich AI reasoning between Python pre-processing and Python post-validation.

Example

from praisonai import Agent, AgentFlow

def normalize_input(ctx):
    return ' '.join(ctx.input.strip().split())

def tag_output(ctx):
    return f'VALIDATED -> {ctx.previous_result.strip()}'

classifier = Agent(name='Classifier', instructions='Reply with one word: billing or technical.', output='silent')
flow = AgentFlow(steps=[normalize_input, classifier, tag_output])
print(flow.start('   DOUBLE   CHARGE   on invoice 88   ')['output'])

Try it Yourself

Example explained

  • normalize_input — Cleans whitespace in <1ms at $0 cost before calling the LLM.
  • verify_json — Deterministic gate validating the agent's JSON structure before returning downstream.

Cut LLM cost by 50%+ with hybrid workflows

Keep formatting, sorting, summing, deduplicating, and schema assertions in plain Python steps — reserve LLM calls purely for ambiguity and language synthesis.

Common pitfall: forgetting to return a string or value from a function step

If a Python step modifies data and forgets return, ctx.previous_result becomes None for the next agent. Always return the transformed value explicitly.

Reference: documentation