Call Serverless Function
Overview
Serverless Functions allow your agents to execute custom code in the cloud without managing infrastructure. This powerful capability enables complex operations and integrations beyond what standard actions can provide.
Use Cases
- Custom Logic Implementation: Execute specialized code for unique business requirements
- External System Integration: Connect with third-party services and APIs
- Advanced Data Processing: Perform complex calculations and transformations
- Extended Functionality: Add capabilities not available in standard Agent.ai actions
How Serverless Functions Work
Serverless Functions in Agent.ai:
- Run in AWS Lambda (fully managed by Agent.ai)
- Support Python and Node.js
- Automatically deploy when you save the action
- Generate a REST API endpoint for programmatic access
Creating a Serverless Function
- In the Actions tab, click “Add action”
- Select the “Advanced” category
- Choose “Call Serverless Function”
Configuration Fields
Language
- Description: Select the programming language for the serverless function.
- Options: Python, Node
- Required: Yes
Serverless Code
- Description: Write your custom code.
- Example: Python or Node script performing custom logic.
- Required: Yes
Serverless API URL
- Description: Provide the API URL for the deployed serverless function.
- Required: Yes (auto-generated upon deployment)
Output Variable Name
- Description: Assign a variable name to store the result of the serverless function.
- Example: “function_result” or “api_response.”
- Validation: Only letters, numbers, and underscores (
_
) are allowed. - Required: Yes
Deploy and Save
- Click “Deploy to AWS Lambda”
- After successful deployment, the API URL will be populated automatically
Using Function Results
The function’s output is stored in your specified variable name. You can access specific data points using dot notation, for example:
{{variable_name.message}}
{{variable_name.input}}
Accessing Agent Variables
When your agent runs a serverless function, any context variables created earlier in the workflow are passed into your function as part of the event object.
Understanding how to access these variables is key to using serverless functions effectively.
Inspecting the Event Structure
To inspect the data being passed to your function, you can set up a basic debug function. This is helpful for confirming that your agent variables are available and structured as expected.
Running your agent with this code will return the full contents of the event and context objects. In most cases, the information you’ll want is located here:
event['body']['context']
This nested context object contains your agent’s variables—such as out_topic, out_summary, and others defined earlier in your workflow.
Accessing Variables in Your Code
Once you understand the structure, you can write your function to access specific values like this:
You can now use these variables to power more complex logic in your serverless functions.
Notes on Debugging
- Use the
return
statement to pass debugging information back to the agent UI.print()
statements will only appear in AWS logs. - The context panel in Agent.ai shows the variables currently available to your serverless function—this can help confirm what’s being passed in.
- If your function isn’t behaving as expected, start by confirming that the data is structured as described above.
Example: Serverless Function Agent
See this simple Message Analysis Agent that demonstrates how to use Serverless Functions:
- Step 1: Get user input text message
- Step 2: Call a serverless function that analyzes:
- Word count
- Character count
- Sentiment (positive/negative/neutral)
- Step 3: Display the results in a formatted output
This sample agent shows how Serverless Functions can extend your agent’s capabilities with custom logic that would be difficult to implement using standard actions alone.