Workflow Workflow
The workflow primitive allows you to create and manage a sequence of steps with advanced features like timeouts, retries, and error handling. It's designed to facilitate complex task orchestration in your AI applications.
Generate a User/Org API key
You will need to generate an API key to authenticate your requests. For more information, visit the User/Org API key documentation.
API reference
new Workflow(config)
Create a new Workflow instance by instantiating the Workflow
class.
Class Instantiation
const workflow = new Workflow(config);
// with types
const workflow = new Workflow(config?: WorkflowConfig);
config
- Name
config
- Type
- WorkflowConfig
- Description
WorkflowConfig Object
interface WorkflowConfig { debug?: boolean; }
Following are the properties of the config object.
debug
- Name
debug
- Type
- boolean
- Description
When set to
true
, detailed execution logs for each step will be printed to the console. This includes step start/end times, timeouts, retries, and any errors encountered.Default:
false
workflow.step(config)
- Name
step
- Type
- Function
- Description
Define and execute a step in the workflow.
step Function Signature
step<T = any>(config: StepConfig<T>): Promise<T>
The
step
function accepts a configuration object and returns a Promise that resolves to the result of the step execution.
StepConfig
- Name
config
- Type
- StepConfig<T>
- Description
StepConfig Object
interface StepConfig<T = any> { id: string; timeout?: number; retries?: RetryConfig; run: () => Promise<T>; }
Following are the properties of the step config object.
id
- Name
id
- Type
- string
- Required
- Required
- Description
A unique identifier for the step. This ID is used in logs and for storing the step's output in the workflow context.
timeout
- Name
timeout
- Type
- number
- Description
Maximum time in milliseconds that the step is allowed to run before timing out. If not specified, the step will run until completion or until it fails.
retries
- Name
retries
- Type
- RetryConfig
- Description
Configuration for retry behavior if the step fails.
RetryConfig Object
interface RetryConfig { limit: number; delay: number; backoff: 'exponential' | 'linear' | 'fixed'; }
- Name
limit
- Type
- number
- Required
- Required
- Description
Maximum number of retry attempts after the initial try.
- Name
delay
- Type
- number
- Required
- Required
- Description
Base delay in milliseconds between retry attempts.
- Name
backoff
- Type
- string
- Required
- Required
- Description
Strategy for increasing delay between retries:
exponential
: Delay doubles with each retry attempt (delay * 2^attempt)linear
: Delay increases linearly with each attempt (delay * attempt)fixed
: Delay remains constant for all retry attempts
run
- Name
run
- Type
- Function
- Required
- Required
- Description
The function to execute for this step. Must return a Promise that resolves to the step result.
run Function Signature
() => Promise<T>
Usage examples
Install the SDK
npm i langbase
Environment variables
.env file
LANGBASE_API_KEY="<USER/ORG-API-KEY>"
LLM_API_KEY="<YOUR-LLM-API-KEY>"
Workflow
examples
workflow
import { Langbase, Workflow } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
const workflow = new Workflow({
debug: true,
});
try {
// Step 1: Fetch data
const data = await workflow.step({
id: 'fetch-data',
run: async () => {
// Simulate API call
return { topic: 'climate change' };
},
});
// Step 2: Generate content using the Agent
const content = await workflow.step({
id: 'generate-content',
timeout: 8000, // 8 second timeout
run: async () => {
const { output } = await langbase.agent.run({
model: 'openai:gpt-4o-mini',
instructions: 'You are a helpful AI Agent.',
input: `Write a short paragraph about ${data.topic}`,
llmKey: process.env.LLM_API_KEY!,
stream: false,
});
return output;
},
});
console.log('Final result:', content);
} catch (error) {
console.error('Workflow failed:', error);
}
}
main();
Example outputs
Example Outputs
🔄 Starting step: fetch-data
⏱️ Step fetch-data: 5.214ms
📤 Output: { "topic": "climate change" }
✅ Completed step: fetch-data
🔄 Starting step: generate-content
⏳ Timeout: 8000ms
⏱️ Step generate-content: 2352.871ms
📤 Output: "Climate change represents one of the most urgent global challenges
of our time. Rising temperatures, shifting weather patterns, and increasingly
frequent extreme weather events are disrupting ecosystems and threatening
communities worldwide. Caused primarily by human activities like burning
fossil fuels and deforestation, climate change requires immediate collective
action through policy reforms, technological innovation, and individual
lifestyle changes to mitigate its worst effects and build a sustainable
future for coming generations."
✅ Completed step: generate-content
Final result: "Climate change represents one of the most urgent global
challenges of our time. Rising temperatures, shifting weather patterns, and
increasingly frequent extreme weather events are disrupting ecosystems and
threatening communities worldwide. Caused primarily by human activities like
burning fossil fuels and deforestation, climate change requires immediate
collective action through policy reforms, technological innovation, and
individual lifestyle changes to mitigate its worst effects and build a
sustainable future for coming generations."