Skip to main content

Configuring a Code Agent

Written by Sam B.

When To Use A Code Agent

  • Use a code agent for data transformations, or typed shaping between steps.

  • Use a simpler Orchestrate, Branch, API, or command flow when the behavior does not need custom TypeScript.

Set Up The Node

  1. Open the automation builder.

  2. Add Code from the add-agent panel.

  3. Select the new code node.

  4. Review Checkpoint, Instructions, Tools, Inputs, and Outputs.

  5. Define the inputs the code will read and the outputs later steps will need.

Treat The Editor As A Function Body

  1. Write TypeScript only.

  2. Treat the editor as the body of an async function, not as a full file. Execution occurs in a linear fashion.

  3. Use the generated globals: inputs, tools, context.

  4. Do not add imports, require, or package-install commands.

  5. Watch the generated types and autocomplete. Magical injects declarations from the current inputs, outputs, shared inputs, and available tools.

const customerId = inputs.customerId as string;  return {   status: 'success',   message: 'Prepared customer lookup',   output: { customerId }, };

Call Another Step From Code

  1. Wire the downstream agent on the canvas.

  2. Use the typed tools object instead of trying to import or invoke steps manually.

  3. The common pattern is await tools.callSpecializedAgent(...) for a callable child agent.

  4. Check the result, then return the next output shape.

const lookup = await tools.callSpecializedAgent({   agent: {     agentId: '<child-agent-id>',     args: { customerId: inputs.customerId },   }, });  if (lookup.status === 'fail') {   return {     status: 'fail',     message: lookup.message,   }; }  return {   status: 'success',   message: 'Customer lookup completed',   output: {     customerSummary: lookup.output,   }, };

Always Return A Final Result

  1. End the code with a return.

  2. Return status: 'success' or status: 'fail'.

  3. Include a clear message.

  4. Include output when the node should hand structured data to later steps.

  5. Magical handles the final reportStop call after your code returns, so your job is to return the final result object instead of calling reportStop yourself.

Fix Validation Errors Before Saving

  1. Save blockers show Code Validation Errors with line numbers.

  2. Type errors come from the generated TypeScript context.

  3. Security errors flag forbidden identifiers such as require, import(), fetch, process, console, and similar globals.

  4. Resolve those errors before saving or publishing.

Team Guardrails

  • No package downloads or package imports inside a code node.

  • Keep code small, typed, and readable enough for another builder to review quickly.

  • Ask for regular code review on code-node work. Treat it as engineering work, not as a black-box prompt.

Important Boundaries

  • A code agent still uses the automation's configured Inputs, Outputs, Tools, and downstream graph.

  • Code nodes live inside the builder route /automations/[automationId]; they do not create a separate script file or package.

  • If a simpler agent or command can do the job, prefer the simpler node.

Did this answer your question?