Code Optimization Agent

This use case demonstrates an automated workflow that reviews and optimizes SAS scripts based on execution logs. It retrieves code and logs, analyzes them to improve performance and maintainability, and then commits the optimized version to a GitLab repository. The workflow showcases how AI can autonomously enhance code efficiency, ensure version control, and streamline data processing tasks.

Use Case

Build an intelligent SAS Code Optimization assistant that reviews SAS scripts and their execution logs, generates optimized versions of the code, and commits the changes to a GitLab repository for governed, auditable deployment.

Prerequisites

  • Access to a GitLab repository containing sample SAS scripts and permissions to create branches/commits.

  • Access to the mock SAS command-line wrapper REST API (or equivalent simulated logs and code endpoints).

  • Basic familiarity with SAS syntax, Git workflows, and the platform interface.

  • Basic familiarity with the platform interface.

Step 1: Create SAS Reviewer Agent Prompt

  1. Navigate to Prompt Playground on the left side.

  2. Click on Add new in top corner on the right side.

  3. Click on Prompt templates on the right side.

  4. Select the SAS Scripts Retrieval and Reviewer Agent template.

  5. Increase the Max State Updates from 3 to 20 for complex reasoning.

  6. Scroll down to Agent input box to add the following test query : "Job id 10002"

  7. Ensure that you scroll up to rename your prompt before saving.

  8. You will be directed to the Prompt Playground and select your prompt:

[Hint: Please Save your Changes before you navigate away while adding new tools ]

  • Click on the Tools tab to configure the following tools:

    1. Rest API Tool

      • Click on Add new in tools.

      • Enter name as “Gitlab API tool”.

      • Enter the Description as “Retrieves details from SAS (Statistical Analysis System) files, including content information. Input requires a valid SAS file path. Returns comprehensive file information. Useful for data analysis and SAS file inspection tasks."

      • Select Type: Rest API.

      • Add this input schema:

  • Enter the required credentials provided separately.

  • Save the tool.

  1. Rest Api Tool

    • Click Add new in tools.

    • Enter name as “Job API Tool”.

    • Enter the Description as “Retrieves detailed information about a specific job using its unique job ID. Input requires a valid job identifier. Returns comprehensive job details including status, file URL, and related metadata. Useful for tracking and monitoring job progress or accessing job information.”

    • Select Type: Rest API.

    • Add this input schema:

  • Enter the required credentials provided separately.

  • Save the tool.

Test & Compare :

  1. Click on Test and compare tab.

  2. Select different models [Hint: Compare Claude Sonnet 4.5, Claude 3.5 Sonnet V2, Claude Haiku 4.5].

  3. Click on Test right below.

  4. Compare the output of the selected Models.

  5. Click on Select as best answer to select the best-performing model as the Primary Model.

  6. Optionally Select as best answer to assign a Fallback Model.

  7. Click Save prompt run in the right corner.

  8. Save and publish the prompt.

Step 2: Create SAS Optimizer Agent Prompt

  1. Navigate to Prompt Playground on the left side.

  2. Click on Add new in top corner on the right side.

  3. Click on Prompt templates on the right side.

  4. Select the SAS Script Optimizer Agent template.

  5. Increase the Max State Updates from 3 to 20 for complex reasoning.

  6. Scroll down to Agent input box to add the following test query.

  1. Ensure that you scroll up to rename your prompt before saving.

  2. You will be directed to the Prompt Playground and select your prompt:

[Hint: Please Save your Changes before you navigate away while adding new tools ]

  • Click on the Tools tab to configure the following tools:

    1. Rest Api Tool

      • Click on Add new in tools.

      • Enter name as “JOB Commit Tool”.

      • Enter the Description as “Creates and adds a new commit to a GitLab repository. Requires optimized SAS file content and commit message. Returns commit status and ID upon successful execution. Used for version control when saving changes to GitLab-hosted code repositories.”

      • Select Type: Rest Api.

      • Add this input schema:

  • Enter the required credentials provided separately.

  • Save the tool.

Test & Compare :

  1. Click on Test and compare tab.

  2. Select different models [Hint: Compare Claude Sonnet 4.5, Claude 3.5 Sonnet V2, Claude Haiku 4.5].

  3. Click on Test right below.

  4. Compare the output of the selected Models.

  5. Click on Select as best answer to select the best-performing model as the Primary Model.

  6. Optionally Select as best answer to assign a Fallback Model.

  7. Click Save prompt run in the right corner.

  8. Save and publish the prompt.

Step 3: Create an Agent 2.0 Recipe

  1. Navigate to the Recipes section.

  2. Click Add New in the top-right corner to create a new recipe.

  3. Configure recipe details:

    1. Name: SAS Automation

    2. Type: Agent 2.0

  4. Set up the workflow :

    1. Drag and drop Webhook node

      • From the right-side panel, copy the Webhook URL and Webhook Token to your notepad for later API use.

      • Select the Query Method as POST.

    2. Drag and drop Start node:

      • Connect the Webhook node to the Start node.

    3. Drag and drop Agent node:

      1. Label the agent node as SAS Scripts Retrieval and Reviewer Agent.

      2. Select your created SAS Scripts Retrieval and Reviewer Agent prompt at Step 1.

      3. Scroll down and Set the Messages dropdown to Last Messages.

      4. Connect the Start node to the SAS Scripts Retrieval and Reviewer Agent node.

    4. Again drag and drop Agent node:

      1. Label the agent node as SAS Script Optimize.

      2. From the agents dropdown, select your SAS Script Optimizer Agent created in Step 2.

      3. Connect the SAS Scripts Retrieval and Reviewer Agent node to the SAS Script Optimizer Agent node.

      4. Click on SAS Script Optimize the agent node.

      5. Scroll down and set the State settings:

        • Messages: Enable Messages

        • Message Context: Node Message

          • Variable: Enter "question" as variable.

          • Node: Select SAS Scripts Retrieval and Reviewer Agent.

          • Message: Select Last Message from the dropdown.

    5. Drag and drop End node:

      1. Connect the SAS Script Optimize Agent node to the End node.

  5. On the right side, set the Number of state updates to a higher value such as 75.

  6. Click on the canvas, then select Save.

  7. Select your recipe from the recipe table.

  8. Ensure you click on Publish the recipe.

  9. By default, the V1 version is deployed once it is published.

  10. For subsequent versions, use the Actions button and select Deploy to deploy the new version. [Hint: Use Actions Button to select Deploy dropdown].

The recipe is shown below

Step 4: Trigger Workflow via API

  1. Open the Linux terminal in your computer.

  2. Use the following curl command to initiate document processing

[Hint: Ensure CURL command input payload is valid JSON].

Step 5: Monitor and Verify Results

  • Navigate to the recipe icon on the LHS toolbar.

  • Click on the Actions button on the SAS Automation recipe.

  • Select Webhook History. Here, you can review the history of all webhook requests along with their inputs, response, status, tokens and detailed traces.

  • Review JSON output.

Last updated