Overview
The Functions Microservice provides a secure, isolated environment for running custom code within the Prisme.ai platform. It supports:- NodeJS Functions: JavaScript code execution with access to selected built-in modules
- Python Functions: Python code execution with standard library access
- Shared Execution Context: Functions within the same workspace can call each other
- Memory Management: Configurable resource limits and garbage collection
- Dependency Management: Automatic installation of required packages
The Functions Microservice is designed with security in mind, running code in isolated environments with configurable resource limits to prevent abuse or resource exhaustion.
Installation Prerequisites
Before deploying the Functions Microservice, ensure you have:Storage Volume
The service requires a volume for storing code, dependencies, and execution data. We recommend using volumes with high I/O performance as they may experience heavy usage when installing dependencies.
Optional: NPM Registry Access
Optional: NPM Registry Access
All JavaScript dependencies needed by the v27 AI Products (Agent Creator, LLM Gateway, Storage, Governe, Insights, …) are bundled into the
prismeai-functions image. On top of that, a set of commonly used JavaScript packages (node-fetch, js-yaml, dayjs, mongodb, redis, sharp, etc.) is pre-installed in the Docker image and shared across all workspaces.No npm registry access is required to run the platform or the AI Products, including in offline / private-network deployments.An npm registry is only needed if you write Custom Code that pulls in additional JavaScript packages beyond the bundled ones. In that case, you can:- Set
NPM_CONFIG_REGISTRYto point to your own internal registry (typical for offline deployments with an internal mirror). - Or set the workspace
disableNpmRegistryoption totrueto forbid any runtimenpm install(only the bundled and pre-installed shared packages will be available).
https://www.npmjs.com/.Configuration
Environment Variables
Configure the Functions Microservice with the following environment variables:Python Configuration
Python Configuration
NodeJS Configuration
NodeJS Configuration
Default NODEJS_SHARED_PACKAGES_WHITELIST_IMPORT
The following packages are pre-installed and shared across all workspaces by default:All transitive dependencies of these packages are also available for import. The effective shared whitelist is auto-detected from the
node_modules/ directory, not just the direct dependencies listed above.Resource Considerations
When deploying the Functions Microservice, consider the following resource recommendations:Memory Allocation
Allocate sufficient memory based on your expected workload:
- Minimum: 1GB
- Recommended: 2GB or more for production environments
- Consider additional memory if functions will process large datasets
CPU Resources
Ensure adequate CPU resources:
- Minimum: 0.5 vCPU
- Recommended: 1 vCPU or more for production environments
- Functions with complex calculations may benefit from additional CPU
Disk Space
Plan for storage requirements:
- Minimum: 1GB
- Recommended: 5GB or more for environments with many dependencies
- Consider storage needs for dependency caching and function code
Network Configuration
Ensure proper network setup:
- Accessible by other microservices (especially API Gateway)
- Access to required external resources (NPM registry, etc.)
- Consider network policies for security
Python Memory Management
Python functions run in a pool of Python processes (KERNEL_POOL_SIZE, 10 by default). On Kubernetes, they run in their own python container of the functions pod, with its own memory limit (python.resources in the prismeai-functions chart, see Kubernetes resources).
To keep this container from running out of memory and restarting, Python functions are kept below 90 % of its memory limit:
- A function needing more memory than what is left fails with
MemoryLimitExceeded, instead of making the container restart. The other functions, Python and JavaScript, keep running. - When the container gets close to its limit all the same, the Python functions using the most memory are stopped with
MemoryLimitExceeded, and idle Python processes that kept memory from earlier runs are restarted, without failing any function. - With
KERNEL_MEMORY_LIMIT_MB, each Python function is also limited on its own, even when the container has memory left. Use it to keep one workspace from taking the memory of the others.
- These protections make a restart of the
pythoncontainer very unlikely, but cannot rule it out when several functions fill memory very fast at the same moment. Even then, the JavaScript functions keep running, in their own container. - Without a memory limit on the container, Python functions are not limited.
- A Python function can use the memory left below 90 % of the container limit, not all of it.
Health Checks
With the Helm chart, each container of the functions pod has its own liveness probe onGET /sys/healthcheck:
nodefails when the functions storage is unusable, withFILESYSTEMstorage only (an S3 outage does not restart the pod). It does not check Python: a Python failure never restarts the JavaScript functions.pythonfails when the Python API stops answering, or when more thanKERNEL_POOL_UNAVAILABLE_PERCENT% of its kernels have been starting for longer thanKERNEL_POOL_UNAVAILABLE_GRACE. Images before 27.9.2 get no probe on this container.
python container. A failure that no restart can fix (a broken package, kernel users lacking permissions) therefore keeps the python container in CrashLoopBackOff, while the JavaScript functions keep running.
When Node and Python share one container (PYTHON_AUTOSTART left to true, outside the Helm chart), the Python API first restarts alone. Only if its kernels stay unavailable after that does the liveness probe fail and restart the whole container, JavaScript functions included.
Each restart of the Python API loses the Python executions in progress or queued at that moment, callbacks included. Keep
KERNEL_POOL_UNAVAILABLE_GRACE at 30 seconds or more, and above the time the whole pool takes to start (many kernels on little CPU): below that, kernels slow to start count as stuck, and Python restarts in a loop.Microservice Testing
After deploying the Functions Microservice, verify its operation with these simple tests:1
Create a Test Function
Create a function workspace named If successful, the same body should be returned in the response.
test containing a hello function:2
Execute the Function
Run the function with a test parameter:If successful, you should receive a response like:
3
Verify Resource Limits
Test that resource limits are enforced by creating a function that attempts to use excessive resources:When executing this function, it should terminate due to resource limits or timeout:
Advanced Features
The Functions Microservice includes several advanced capabilities that enhance its flexibility and power:Asynchronous Functions
Asynchronous Functions
All functions are executed as asynchronous operations, even if they don’t explicitly use async calls. This means:
- Every function is treated as if it had the
asynckeyword - When calling functions from the same workspace, you must use the
awaitkeyword - Results are always returned as promises
Dependency Management
Dependency Management
Functions can use external dependencies by specifying them in the function definition:
- NodeJS functions can use NPM packages. Dependencies are resolved in the following order:
- Shared dependencies (pre-installed at build time): A set of commonly needed packages are bundled in the Docker image under
SHARED_NODE_MODULES_DIRECTORYand available to all workspaces instantly, without any npm install at runtime. These include packages likelodash,axios,dayjs,mongodb,redis,sharp, and more. - Per-workspace dependencies: Any additional package required by a workspace’s custom code that is not in the shared dependencies will be automatically installed via npm when the function is created or updated. This can be disabled per workspace with the
disableNpmRegistryoption.
- Shared dependencies (pre-installed at build time): A set of commonly needed packages are bundled in the Docker image under
- Python functions can use PyPI packages that have been whitelisted and pre installed with the corresponding environment variables
NODEJS_PACKAGES_WHITELIST_IMPORT is set, shared packages are automatically added to the effective whitelist so that platform-native custom code continues to work without requiring clients to explicitly list every shared package.Example NodeJS function with dependencies:Integration with Prisme.ai
The Functions Microservice integrates with other Prisme.ai components:Builder
Within Builder, you can create custom automations that execute functions, allowing you to extend workflows with custom logic.
Knowledges
Custom functions can be used as tools in Knowledges agents, enabling them to perform specialized operations like data transformation or external API calls.
Agent Creator
Functions can power features in agents published to the Agent Creator, adding custom capabilities to shared agents.
Custom Code App
The Custom Code app provides a user-friendly interface for managing, testing, and monitoring functions.
Security Considerations
When deploying and using the Functions Microservice, keep these security considerations in mind:Code Isolation
Functions run in isolated environments to prevent interference between different workspaces and functions.
Resource Limits
Configure appropriate resource limits to prevent denial-of-service attacks or resource exhaustion.
Access Control
Implement proper authentication and authorization to control who can create and execute functions.
Dependency Scanning
Consider scanning dependencies for vulnerabilities before allowing their installation.
Troubleshooting
Function Creation Fails
Function Creation Fails
Possible causes:
- Invalid function code syntax
- Unavailable or incompatible dependencies
- Insufficient disk space for dependencies
- Network issues preventing access to the NPM registry
- Check the service logs for specific error messages
- Verify NPM registry access from within the container
- Ensure sufficient disk space in the function storage volume
- Validate function syntax before submission
Function Execution Timeouts
Function Execution Timeouts
Possible causes:
- Function code contains infinite loops or excessive processing
- Timeout setting is too low for the operation
- External service calls that take too long
- Review function code for inefficiencies or infinite loops
- Adjust the
FUNCTIONS_RUN_TIMEOUTorPYTHON_FUNCTIONS_RUN_TIMEOUTsetting - Implement better error handling for external service calls
- Consider breaking complex operations into smaller, chainable functions
Functions Pod Restarts or CrashLoopBackOff
Functions Pod Restarts or CrashLoopBackOff
Possible causes:
pythoncontainer: Python kernels that cannot start, because of a broken package inPYTHON_PACKAGES_AUTO_INSTALL, aKERNEL_POOL_SIZEabove 23 (the image only creates 23 kernel users), or too little CPU for the poolnodecontainer: functions storage volume unusable (FILESYSTEMstorage)
- Find which container restarts:
kubectl describe pod <functions pod>gives the restart count of each - For
python, search its logs forKernel pool unavailable,still not ready afteranddied, restarting it - Check the packages listed in
PYTHON_PACKAGES_AUTO_INSTALLand the CPU allocated to thepythoncontainer - See Health Checks for what each liveness probe checks