AI Agents

An AI agent is a system that uses a model to choose actions in an environment. Agents often combine reasoning, memory, tools, retrieval, and feedback so they can complete multi-step tasks.

When should a workflow allow loops?

A workflow should allow loops when a task genuinely benefits from repeating a step after receiving new information—and when the system can recognize progress and know when to stop. Loops are appropriate for controlled improvement or recovery, not as a default for every agent action. When is a loop useful? Validation driven correction: A generated artifact fails a measurable check and needs a targeted revision. Recoverable failures: A tool call fails temporarily, and another attempt could succeed after a delay or changed input. Incremental discovery: New evidence identifies a specific unanswered question that must be investigated. Processing a bounded collection: An agent handles remaining records or tasks until the collection is complete. Human feedback: An approved reviewer requests a change, followed by another validation step. Each iteration should have a reason: new feedback, updated state, or a remaining work item. Example: An API test generation agent An agent generates tests for an endpoint and runs a coverage validator. 1. Generate tests: Produce cases from the endpoint requirements. 2. Validate coverage: Check required status codes, boundary conditions, and authorization scenarios. 3. Decide: If required coverage passes, finish . If a specific gap is fixable, revise only the affected tests and validate again. If the specification is missing essential information, request clarification rather than guessing. If the revision budget is exhausted, stop and report unresolved gaps . For example, validation finds that the 401 response is not tested. The agent adds the missing case and reruns coverage. Once the check passes, another revision is unnecessary. When should a workflow avoid loops? The task is deterministic and can be completed in one pass. Repetition produces no new evidence or meaningful improvement. The failed operation is not safely repeatable, such as an unverified payment or message send. Required authorization is absent. The same error persists without a recovery strategy. The cost of additional iterations exceeds the expected benefit. For transient errors, a bounded retry with backoff may be more appropriate than a full reasoning and replanning loop. How do you decide whether to loop again? Use a clear decision rule: Is a required condition still unmet, is there an actionable change, and is another attempt allowed? A safe loop should have measurable acceptance criteria, a maximum attempt count, time or cost limits, and a failure or escalation path. Keep the iteration history so repeated outputs or unchanged failures are detectable. In short Allow loops when feedback can drive a useful next attempt. Exit when the goal is achieved, progress stalls, a limit is reached, or continuing would be unsafe.

How do you control loops in an agent graph?

You control loops in an agent graph by defining when the workflow may repeat, what changes on each iteration, and exactly when it must exit. The loop should be governed by explicit state and routing rules rather than an open ended instruction to “keep trying.” Example: Fixing generated API tests An agent generates tests, validates coverage, and revises only missing cases: Generate tests → Validate → Decide Validation passed: Exit to the report node. Fixable gap and attempts remain: Revise the tests, increment the attempt counter, and return to validation. No progress, unsafe retry, or limit reached: Exit with a clear failure or request human input. Suppose validation discovers that a required 401 Unauthorized test is missing. One revision adds the case. If validation passes afterward, the loop stops; the agent does not revise the test suite again. Six practical controls 1. Track iteration state. Store the attempt count, latest validation result, remaining gaps, and previous error. Do not rely on conversation history alone for operational counters. 2. Use explicit routing conditions. Choose between continue , success , and stop/escalate from validated state, not unrestricted model generated commands. 3. Set a hard iteration limit. For example, permit at most three revisions for a particular task. The suitable value depends on its cost and risk. 4. Enforce resource budgets. Apply deadlines, token or tool call limits, and cancellation rules in addition to the attempt counter. 5. Detect stalled progress. Stop if repeated iterations return the same error, produce unchanged results, or fail to reduce measurable gaps. 6. Protect side effects. Before retrying a payment, notification, or database mutation, check whether the previous attempt succeeded. Use idempotency keys or reconciliation when supported. What does a graph router check? A conceptual routing rule is: If all acceptance criteria pass , go to END . Otherwise, if attempts are below the limit , a meaningful fix is available , and budgets remain , go to REVISE . Otherwise go to STOP OR ESCALATE . For frameworks such as LangGraph, the iteration counter and status can be stored in graph state, with a conditional edge choosing the next node. Runtime recursion or step limits provide an additional guardrail, but are not substitutes for business level success and failure conditions . Common mistakes Incrementing the counter only after a successful retry, so failures never consume the budget. Repeating an LLM call without new feedback or changed inputs. Checking only the maximum iteration count while ignoring wall clock and API cost limits. Treating a timed out external action as safe to repeat automatically. Exiting after a fixed number of tries without reporting whether the goal was achieved. In short A controlled agent loop has a measurable goal, bounded retries, progress checks, safe state transitions, and a reliable exit path. These controls make iterative workflows useful without allowing infinite execution or accidental duplicate actions.

What is workflow state in an agent system?

Workflow state is the structured information an AI agent workflow keeps about a particular run so each step can use earlier results and know what has happened. It is the workflow's current working record, not the AI model's internal memory. Example: API test generation agent An agent generates tests for a single API endpoint. Its workflow state might contain: endpoint: /orders/{id} requirements: Verified request, response, and authentication rules extracted from the specification test cases: Generated test scenarios validation status: pending, passed, or failed missing coverage: Any required cases not yet addressed revision count: Number of correction attempts error: Most recent failure, if applicable The requirements node adds the relevant specification details. The test generation node reads those details and adds test cases. The validation node then records whether the tests meet the requirements. Instead of rediscovering everything at each step, nodes use the shared state as a consistent record of the run. What belongs in workflow state? Inputs: The user request, task identifiers, and necessary constraints. Intermediate outputs: Results from tools, model calls, and validation steps. Control information: Completion status, retry counts, branch decisions, and remaining work. References: Identifiers or pointers to large documents and artifacts stored elsewhere. Errors: Structured failure details needed for recovery and debugging. Keep state focused. Large documents, raw logs, or secrets should not be copied into it unnecessarily. How is state different from memory? Workflow state usually belongs to a particular execution and changes as the workflow progresses. Agent memory is information deliberately retained for reuse beyond the current step or run, such as a persisted preference or a knowledge store entry. State may be checkpointed and restored, but that does not automatically make it long term memory. Likewise, a chat message history can be one field in workflow state; it is not the whole state. How do graph nodes use it? In a framework such as LangGraph, a state schema defines fields available to the graph. Nodes read current values and return updates. The framework applies those updates according to the configured state channels and reducers. For example, a validation node can update validation status to failed and write missing coverage . A conditional route then reads those fields to decide whether to revise the tests or finish. When branches run concurrently, updates to shared fields need an explicit merge strategy so results are not lost or overwritten unexpectedly. Production considerations Give every execution a distinct run identifier and isolate data between users or tenants. Define typed state fields and validate node outputs. Avoid storing credentials or unnecessary personal information in checkpoints and logs. Use durable checkpoints when interruption and resumption are required. Distinguish persisted workflow state from external side effects; restoring a checkpoint does not undo a payment or API mutation. In short Workflow state connects the steps of an agent system by recording inputs, results, decisions, and progress for each run. Well designed state makes execution reliable, debuggable, and resumable.

How is state passed between workflow nodes?

State is passed between workflow nodes through a shared, structured workflow state that the orchestration runtime makes available to each node. A node reads the fields it needs, performs its work, and returns updates. The runtime applies those updates before the next dependent node runs. Example: An API test generation workflow Suppose a workflow has three nodes: 1. Requirements node: Reads an API specification and produces endpoint specific requirements. 2. Test generation node: Reads those requirements and produces proposed test cases. 3. Validation node: Reads the requirements and test cases, then records coverage gaps or a passing result. The information flows as: Initial state → Requirements node → Updated state → Test generation node → Updated state → Validation node For example, the state might start with endpoint = /orders/{id} . The first node adds requirements . The second adds test cases , preserving the requirements. The third adds validation status and any missing coverage . How is this implemented in LangGraph? In a LangGraph style state graph: A state schema defines fields such as endpoint, requirements, test cases, and validation status. Each node receives the current state as input. A node usually returns a dictionary of state updates rather than manually sending its output to every downstream node. The framework merges those updates according to each field's configured update or reducer behavior. The next eligible node reads the resulting state. Conceptually: Here, generate tests updates only test cases . It does not need to recreate the existing endpoint or requirements fields. What if nodes execute in parallel? Parallel branches may return updates at the same time. If they update different fields, those updates can often be combined directly. If they write the same field, the workflow must define an appropriate reducer or merging rule; otherwise conflicting concurrent updates can fail or produce incorrect results. For example, security test and input validation branches can write to separate fields, or append to a shared collection using a properly configured reducer. What about Celery or separate workers? A local graph runtime can pass state internally. When workflow steps run in separate processes or workers, the orchestrator needs a reliable way to serialize, persist, and retrieve the required state or artifact references. A Celery task's return value alone does not automatically become another graph node's shared state. Large files should normally be stored externally, with their identifiers passed through state rather than repeatedly copying their full contents. Best practices Define clear, typed contracts for the state fields that nodes read and write. Return focused updates rather than accidentally replacing unrelated state. Use stable run identifiers to isolate concurrent users and workflow executions. Configure merge rules for fields written by parallel branches. Checkpoint important state for reliable recovery. Avoid putting secrets or unnecessary sensitive data into persisted state. In short Nodes communicate by reading and updating the workflow's state. The orchestration runtime carries those updates forward, allowing later nodes to use earlier results without tightly coupling every step to the next.

How do you route execution between agent nodes?

Routing execution between agent nodes means deciding which workflow node should run next using the current state and a set of allowed transitions. The routing decision connects a completed step to the correct next action. Example: An API test generation agent Consider an agent with these nodes: 1. Generate tests: Draft tests from endpoint requirements. 2. Validate tests: Check syntax, assertions, and required coverage. 3. Revise tests: Fix specific validation gaps. 4. Publish report: Return the validated test suite. 5. Stop or escalate: Report an unrecoverable error or request missing information. After validation, the router reads structured state such as validation status , missing coverage , and attempt count . If validation passes , route to Publish report . If validation fails with fixable gaps and attempts remain, route to Revise tests . If requirements are incomplete or retries are exhausted, route to Stop or escalate . This prevents an invalid test suite from reaching publication. How does routing work? 1. Read state: Inspect validated outputs and the current workflow status. 2. Evaluate a rule or decision: Determine which permitted transition matches the result. 3. Select a destination: Return a valid next node identifier or a terminal outcome. 4. Execute and record: The orchestrator activates that node and logs the chosen path. Some transitions are unconditional : a requirements node always proceeds to test generation when successful. Others are conditional : a validation result determines which branch to take. Example with LangGraph The following conceptual Python router chooses a node based on state: With conditional edges, LangGraph can map these allowed outcomes to downstream nodes. The exact mapping and state schema are defined when constructing the graph. Rules based or model assisted routing? Rules based routing is suitable for objective conditions such as status codes, approvals, and retry limits. It is predictable and testable. Model assisted routing can help interpret ambiguous requests or select specialist tools. Treat model output as a recommendation: validate it against an allowlist of routes and never let it bypass authorization or mandatory checks. Common routing mistakes Sending an unrecognized model generated destination directly to execution. Routing based on an error message instead of a structured outcome. Omitting a fallback for unexpected or missing state. Creating a revision loop without a stopping condition. Starting a dependent node before required outputs are available. In short Routing connects agent decisions to workflow execution. Explicit transition rules, validated state, and safe fallback paths help ensure the correct node runs next without losing control of the process.

What is deterministic routing in an agent workflow?

Deterministic routing in an agent workflow means selecting the next node using explicit, predefined rules rather than asking a language model to decide. For the same valid input state and the same rules, the router chooses the same destination. Example: An API test generation agent An agent generates tests and checks them against the endpoint requirements. After validation, the workflow needs to decide what happens next: Passed: Send the verified test suite to the reporting node. Failed with a fixable gap and attempts remaining: Send the tests to a revision node. Missing requirements or exhausted attempts: Stop or request human input. The router uses structured fields such as validation status , fixable , and attempt count . No LLM call is needed to choose among these clearly defined outcomes. How does it work? 1. A node finishes and writes validated results into workflow state. 2. A routing function evaluates predefined conditions against that state. 3. The function returns one of the permitted destinations. 4. The orchestrator executes the corresponding next node or ends the run. For example, in Python: A framework such as LangGraph can use this routing function with conditional edges to direct graph execution. When should deterministic routing be preferred? Objective outcomes: A schema validator returns passed or failed. Retry limits: Another attempt is allowed only when a counter is below its limit. Authorization checks: A sensitive action proceeds only when a trusted permission or approval check succeeds. Known error categories: Timeouts, malformed inputs, and permanent failures follow different defined recovery paths. Business rules: A transaction above a configured threshold requires human review. These are easier to test and audit as explicit rules than as natural language model decisions. How is it different from model based routing? Deterministic routing applies known rules to structured state. Model based routing uses model reasoning to interpret ambiguous inputs or recommend a destination. An agent can combine them: an LLM classifies a support request into a validated category, while deterministic rules check permissions and choose the allowed downstream workflow. The overall system is not fully deterministic if the classification can vary. Advantages and limitations Advantages: Predictable decisions, lower model costs, easier debugging, clearer audit trails, and fewer opportunities for unsupported routes. Limitations: Rules must be maintained as requirements change. They are less suitable for open ended decisions where the necessary conditions cannot be expressed reliably in advance. Best practices Validate state before routing, cover every expected outcome, define a safe fallback for unknown values, and log the decision inputs and selected path. Do not use untrusted model or tool text directly as proof of authorization. In short Deterministic routing makes workflow transitions predictable by applying explicit rules to validated state. It is especially valuable for status checks, safety gates, retry policies, and other decisions where correctness matters more than flexible interpretation.

What is model-based routing in an agent workflow?

Model based routing uses an AI model to decide which workflow path or specialized agent is most appropriate for the current task. It is useful when the decision requires interpreting natural language, intent, or context that cannot easily be captured with simple rules. Practical example: An API test generation assistant A user asks: “Check whether the payment endpoint exposes sensitive data and generate relevant tests.” A routing model examines the request and proposes the security testing branch rather than the general functional testing branch. Another request, such as “Verify status codes and response schemas,” may be routed to functional testing . The model recommends a route ; the orchestrator checks that its output is valid and authorized before starting the corresponding node. How does it work? 1. Define permitted destinations: For example, security tests , functional tests , requirements lookup , and clarify . 2. Provide relevant context: Pass the user request and necessary workflow state fields to the routing model. 3. Request a structured choice: Ask the model to return one allowed route, optionally with a short justification. 4. Validate the result: Reject unknown destinations, invalid formats, and routes that do not meet permissions or prerequisite checks. 5. Execute or fall back: The orchestrator activates the approved node, or safely requests clarification if the decision is uncertain. Simple conceptual example Here, model.classify is illustrative pseudocode, not a built in LangGraph API. In a real implementation, validate a structured model response and apply independent permission checks before routing. When should you use it? Ambiguous requests: The user's wording requires interpretation. Skill selection: Different agents or tools specialize in different tasks. Contextual classification: Routing depends on the meaning of a document or query. Flexible workflows: You cannot exhaustively define every language pattern with fixed rules. How is it different from deterministic routing? Deterministic routing maps validated state to a destination with explicit rules. Model based routing makes a prediction or judgment and may return a different route for similar wording. Because model outputs are probabilistic and can be wrong, use deterministic checks for authorization, retry budgets, required approvals, and allowed destinations. Common risks and safeguards Wrong classification: Evaluate routing accuracy on representative requests, including ambiguous cases. Unexpected output: Restrict destinations with a schema or allowlist and provide a safe fallback. Prompt injection: Treat user and tool content as data, never as authority to bypass routing policy. Higher latency and cost: Use a lightweight classifier or deterministic rules for straightforward cases. Unnecessary tool access: Enforce tool permissions in code, independent of the routing model. In short Model based routing brings flexible interpretation to agent workflows. It works best when the model selects among clearly defined options while the orchestrator enforces boundaries, permissions, and safe execution.

How can multiple branches run concurrently?

Multiple branches in an agent workflow can run concurrently when their inputs are ready and they do not depend on each other’s outputs. The orchestrator starts each eligible branch, manages resource limits, and waits for the results required by the next step. Example: Generating API tests Suppose an agent has already extracted the requirements for an endpoint. It can generate three independent sets of tests: Authentication branch: Check missing, invalid, and insufficient credentials. Input validation branch: Check required fields, invalid types, and boundary values. Response contract branch: Check expected status codes and response schemas. Instead of running these branches one after another, the workflow starts all three after the requirements are available. Read requirements → Run three branches concurrently → Merge results → Validate the final suite This pattern is often called fan out (start several branches) followed by fan in (bring their outputs together). How is concurrent execution implemented? 1. Identify independent tasks. Confirm no branch needs an unfinished sibling branch’s output. 2. Provide inputs. Give each branch the relevant validated requirements and its own working data. 3. Schedule branches. Use the workflow runtime or task executor to start eligible operations without unnecessary waiting. 4. Limit concurrency. Respect model provider rate limits, database capacity, worker pools, and cost budgets. 5. Keep results separate. Store outputs under distinct state keys or use an explicit merge/reducer strategy. 6. Continue after prerequisites complete. Trigger a downstream node only when the required branch results are ready. What does this look like in practice? In a LangGraph style system, a node can fan out to multiple downstream nodes. They can execute in the same graph step, with state updates combined according to the configured reducers. The downstream join must be designed around the actual branch dependencies. With Celery, independent jobs can run on available workers. A group can dispatch jobs concurrently, and a chord can coordinate their completion before a callback. The Celery primitives do not replace the agent workflow’s validation, permissions, or state management rules. What can go wrong? Shared state collisions: Two branches overwrite the same field without a reducer. Overload: Too many requests hit an external API or exhaust worker capacity. Partial failure: One branch fails while others succeed; the workflow needs a defined recovery or partial result policy. Unsafe side effects: Concurrent operations change the same external record or perform duplicate actions. Incorrect joining: A report starts before all mandatory branch results have arrived. Concurrent execution is not automatically parallel CPU computation: asynchronous I/O may overlap on one worker, while multiple processes or workers can execute tasks physically in parallel. In short Run branches concurrently when their work is independent, and coordinate their outputs before dependent steps continue. Explicit state isolation, resource limits, and completion checks keep concurrent agent workflows reliable.

How do you join results from parallel agent branches?

Joining results from parallel agent branches means collecting the outputs of independent tasks and combining them into a validated result before the next dependent step runs. This is the fan in part of a fan out/fan in workflow. Example: An API test generation agent Suppose one endpoint needs three types of tests: Authentication branch: Generates cases for missing, expired, and insufficient credentials. Input validation branch: Generates tests for required fields and invalid values. Response contract branch: Generates assertions for status codes and response schemas. All three branches start after the API requirements have been extracted. A join step waits for their required outputs and creates one test suite. Extract requirements → Run three test branches → Join results → Validate combined suite The join does more than concatenate text: it should preserve the source of each test, remove duplicates, detect contradictory assertions, and verify that the final suite meets the required coverage. How does joining work? 1. Identify expected branches. Track which outputs are required for this endpoint. 2. Wait at a synchronization point. Do not run the final validation node until all mandatory results are available or a defined failure policy applies. 3. Collect structured outputs. Prefer separate fields or records such as authentication tests, input tests, and contract tests. 4. Merge deliberately. Combine test lists, resolve overlapping cases, and preserve stable IDs and provenance. 5. Validate the aggregate. Check schema consistency, missing branches, conflicting assumptions, and overall test coverage. 6. Continue or recover. Publish only a valid aggregate; otherwise retry a failed branch, report partial results, or escalate according to policy. What does LangGraph do? LangGraph can schedule parallel nodes and combine their state updates. If several nodes write to the same state key , configure an appropriate reducer (for example, list accumulation) rather than relying on accidental overwrite. A downstream node can act as the join when its graph dependencies are satisfied. A state reducer merges values; it is not, by itself, a guarantee that every required branch has finished. Synchronization and state merging are separate concerns. For predictable results, consider collecting outputs under unique branch keys, then combining them in a dedicated validation node. If order matters, sort results by explicit branch or test IDs instead of completion time. What about Celery workers? When independent jobs run in Celery, a group dispatches the tasks and a chord can run a callback after the group finishes successfully. The callback can validate and combine the branch outputs. Configure result storage appropriately, and handle task failures and timeouts: successful completion is not guaranteed. Common mistakes Starting the final report after only the fastest branch finishes. Assuming completion order is stable. Overwriting shared state without reducers. Silently ignoring a failed mandatory branch. Merging contradictory tests without checking their requirements. Retrying aggregation in a way that duplicates database writes or external actions. In short A reliable join synchronizes required branches, merges their structured outputs, and validates the combined result. It lets agents benefit from concurrent execution without sacrificing correctness.

How should errors propagate through an agent workflow?

Errors in an agent workflow should propagate as clear, structured failure information from the node where they occur to the part of the workflow responsible for recovery, escalation, or reporting. A failed node must not silently look like a successful one. Example: API test generation workflow An agent extracts requirements, generates API tests, executes them, and prepares a report. During execution, its test runner cannot reach the staging service. The execution node records an error such as: source: test execution category: dependency unavailable retryable: true attempt: 1 message: Staging endpoint could not be reached The orchestrator can now make an informed decision: 1. Retry the operation if the error is transient and a retry is safe. 2. Stop or escalate if attempts are exhausted or the dependency remains unavailable. 3. Report partial progress without claiming the tests passed. If the failure were instead a missing authorization grant, retrying the same request would generally be inappropriate. The workflow should request an approved credential or escalate. How do errors move between nodes? Detection: A node or tool identifies a failure. Classification: Convert the failure into a consistent error category with context. State update or exception: Record the error in workflow state, or allow an exception to reach the workflow runtime for its configured failure handling. Routing: Choose an allowed recovery node, terminal failure, or human review path. Reporting: Expose the outcome clearly, preserving what succeeded and what did not. An error record can contain run id, node, error type, retryable, attempts, timestamp, and sanitized details . Keep secrets, raw credentials, and sensitive payloads out of error messages and logs. Local recovery vs. workflow failure Local recovery makes sense when a node can handle a known problem safely—for example, correcting a malformed generated test after a validator identifies the exact issue. Workflow level failure is appropriate when the error prevents downstream steps from meeting their requirements—for example, a missing mandatory API specification. In that case, dependent nodes should not continue with incomplete or fabricated inputs. For parallel branches, decide in advance whether a failed branch is mandatory (the join fails or waits for recovery) or optional (the join may proceed with an explicitly marked partial result). Practical implementation tips Use a small, documented set of error categories rather than relying on free form text. Keep the original failure context and record each recovery attempt. Bound retries with backoff, deadlines, and attempt limits. Check for completed external side effects before retrying a timed out operation. Make terminal states explicit: succeeded , failed , cancelled , or needs review . Include trace or correlation identifiers so failures can be followed across graph nodes and Celery workers. In LangGraph, nodes can return structured error state updates for conditional routing; unhandled exceptions can also interrupt execution according to the graph runtime and invocation setup. In Celery, failed tasks need an explicit orchestration policy rather than assuming their results will arrive normally. In short Propagate errors with enough context to choose a safe next action, but never hide failures or allow dependent nodes to treat them as successes.