When building workflows, it’s important to understand how side effects are handled. The workflow definition function (the code inside the .define() block) may be executed multiple times during a single workflow execution.

The agents you define in your workflow are durable and will only run once per workflow execution, per agent configuration. Therefore, multiple executions of the workflow definition will not run the same agent multiple times.

Understanding Multiple Executions

Due to the durable nature of workflows, your workflow code might run multiple times. This means that any side effects placed directly in the workflow definition could execute more than once.

Here’s an example:

In this example, both the console.log and sendEmail operations could execute multiple times for the same workflow execution, which is usually not what you want.

To handle this, Inferable provides two different helpers. Which one you choose depends on your use case. See Effects and Results for more details.

Mixing in your own control flow

Unless you know what you’re doing, and have a deep understanding of how Inferable internals work, you should avoid mixing in your own control flow. After all, it’s your codebase, and the workflow “just runs” your code.

If you do want to embark on this wild adventure, here are some things to keep in mind:

  • Be doubly sure that Effects and Results are not a better fit for your use case.
  • Handle errors and failures gracefully. If you interrupt a workflow, it will result in undefined behavior.
  • Bring enough water, a first-aid kit, and a sense of humor.