Skip to content

Run Context (ctx)

The ctx (context) object provides information about the current run. It is passed as the second argument to every task's run function and does not change during execution.

Usage

ts
import { task } from "@trigger.dev/sdk";

export const myTask = task({
  id: "my-task",
  run: async (payload: { message: string }, { ctx }) => {
    console.log(ctx.run.id);           // run ID
    console.log(ctx.attempt.number);   // attempt number
    console.log(ctx.environment.type); // "PRODUCTION" | "STAGING" | etc.
  },
});

Context Properties

ctx.task

PropertyTypeDescription
idstringTask identifier
exportNamestringExported function name
filePathstringFile path of the task

ctx.attempt

PropertyTypeDescription
idstringExecution attempt ID
numbernumberAttempt number (1 = first try)
startedAtDateWhen this attempt started
statusstringCurrent attempt status
backgroundWorkerIdstringWorker ID

ctx.run

PropertyTypeDescription
idstringRun ID
tagsstring[]Tags attached to this run
isTestbooleanWhether this is a test run
createdAtDateRun creation time
startedAtDateRun start time
idempotencyKeystring?Idempotency key if set
maxAttemptsnumberMax retry attempts allowed
durationMsnumberDuration in ms (fixed at call time)
costInCentsnumberCost in cents (fixed at call time)
versionstringCode version this run is locked to
maxDurationnumber?Max allowed duration in seconds

ctx.queue

PropertyTypeDescription
idstringQueue ID
namestringQueue name

ctx.environment

PropertyTypeDescription
idstringEnvironment ID
slugstringEnvironment slug
typestringPRODUCTION | STAGING | DEVELOPMENT | PREVIEW
branchNamestring?Branch name (preview environments only)

ctx.git

PropertyDescription
commitShaFull commit SHA
commitRefBranch or tag ref
commitMessageCommit message
commitAuthorNameAuthor name
dirtyWhether there are uncommitted changes
remoteUrlGit remote URL
pullRequestNumberPR number (if applicable)
pullRequestTitlePR title
pullRequestStateopen | closed | merged

ctx.organization

PropertyDescription
id, slug, nameOrganization identifiers

ctx.project

PropertyDescription
id, ref, slug, nameProject identifiers

ctx.machine

PropertyDescription
nameMachine preset name (e.g., small-1x)
cpuCPU allocation (vCPU)
memoryMemory allocation (GB)
centsPerMsCost per millisecond

ctx.batch

PropertyDescription
idBatch ID (if run was triggered via batch)

Common Patterns

Checking Attempt Number

ts
run: async (payload, { ctx }) => {
  if (ctx.attempt.number > 1) {
    console.log("This is a retry attempt");
  }
}

Environment-Specific Logic

ts
run: async (payload, { ctx }) => {
  if (ctx.environment.type === "PRODUCTION") {
    await sendRealEmail(payload);
  } else {
    console.log("Skipping email in non-production environment");
  }
}

Logging with Run Context

ts
run: async (payload, { ctx }) => {
  logger.info("Starting task", {
    runId: ctx.run.id,
    attempt: ctx.attempt.number,
    env: ctx.environment.type,
  });
}

Built from official Trigger.dev documentation