Skip to main content
The Functions Microservice enables you to execute custom code (NodeJS or Python) within your Prisme.ai applications. This powerful capability allows you to extend the platform with custom logic, integrate with external systems, and implement complex business processes.

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.
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_REGISTRY to point to your own internal registry (typical for offline deployments with an internal mirror).
  • Or set the workspace disableNpmRegistry option to true to forbid any runtime npm install (only the bundled and pre-installed shared packages will be available).
By default, when neither is set, the service tries to install any additional whitelisted packages from https://www.npmjs.com/.

Configuration

Environment Variables

Configure the Functions Microservice with the following environment variables:

Default PYTHON_BUILTIN_MODULES

The following stdlib modules are allowed by default, organized by category:
The following stdlib modules are excluded by default for security reasons: subprocess, os, sys, shutil, socket, ctypes, multiprocessing, _thread, signal, resource, importlib, runpy, code, ast, dis, pickle, marshal, pty, fcntl, mmap, gc, inspect, webbrowser, ensurepip, venv.

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 python container 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 on GET /sys/healthcheck:
  • node fails when the functions storage is unusable, with FILESYSTEM storage only (an S3 outage does not restart the pod). It does not check Python: a Python failure never restarts the JavaScript functions.
  • python fails when the Python API stops answering, or when more than KERNEL_POOL_UNAVAILABLE_PERCENT % of its kernels have been starting for longer than KERNEL_POOL_UNAVAILABLE_GRACE. Images before 27.9.2 get no probe on this container.
The Python API does not wait for its probe: once its kernels stay unavailable, it stops them and exits, and Kubernetes restarts the 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.
The probes do not detect a package install that hangs, a kernel that stays busy, or a package that only fails once a function imports it.

Microservice Testing

After deploying the Functions Microservice, verify its operation with these simple tests:
1

Create a Test Function

Create a function workspace named test containing a hello function:
If successful, the same body should be returned in the response.
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:
If all tests pass, congratulations! Your Functions Microservice is up and running correctly.

Advanced Features

The Functions Microservice includes several advanced capabilities that enhance its flexibility and power:
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 async keyword
  • When calling functions from the same workspace, you must use the await keyword
  • Results are always returned as promises
Example:
Functions within the same workspace are executed within a generated JavaScript file that combines all functions. This allows:
  • Functions to call each other directly
  • Shared utility functions across the workspace
  • Code reuse and modularization
Example:funcA:
funcB:
When funcB is called, it will internally call funcA and return “hello world”.
Functions from the same workspace are executed within the same worker NodeJS and VM2 instance. This enables memory sharing using a special cache variable:
  • The cache object persists between function executions
  • Use it to store and retrieve values across multiple calls
  • Useful for maintaining state or caching expensive operations
Example:funcA:
funcB:
Each call to funcA will increment the counter, and funcB can access the current value.
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:
    1. Shared dependencies (pre-installed at build time): A set of commonly needed packages are bundled in the Docker image under SHARED_NODE_MODULES_DIRECTORY and available to all workspaces instantly, without any npm install at runtime. These include packages like lodash, axios, dayjs, mongodb, redis, sharp, and more.
    2. 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 disableNpmRegistry option.
  • Python functions can use PyPI packages that have been whitelisted and pre installed with the corresponding environment variables
When 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.
Function code is executed in a restricted environment, but it still has access to specified Node.js built-in modules and network resources. Be careful when allowing users to create functions, and consider implementing additional security controls for sensitive environments.

Troubleshooting

Possible causes:
  • Invalid function code syntax
  • Unavailable or incompatible dependencies
  • Insufficient disk space for dependencies
  • Network issues preventing access to the NPM registry
Resolution steps:
  1. Check the service logs for specific error messages
  2. Verify NPM registry access from within the container
  3. Ensure sufficient disk space in the function storage volume
  4. Validate function syntax before submission
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
Resolution steps:
  1. Review function code for inefficiencies or infinite loops
  2. Adjust the FUNCTIONS_RUN_TIMEOUT or PYTHON_FUNCTIONS_RUN_TIMEOUT setting
  3. Implement better error handling for external service calls
  4. Consider breaking complex operations into smaller, chainable functions
Possible causes:
  • python container: Python kernels that cannot start, because of a broken package in PYTHON_PACKAGES_AUTO_INSTALL, a KERNEL_POOL_SIZE above 23 (the image only creates 23 kernel users), or too little CPU for the pool
  • node container: functions storage volume unusable (FILESYSTEM storage)
Resolution steps:
  1. Find which container restarts: kubectl describe pod <functions pod> gives the restart count of each
  2. For python, search its logs for Kernel pool unavailable, still not ready after and died, restarting it
  3. Check the packages listed in PYTHON_PACKAGES_AUTO_INSTALL and the CPU allocated to the python container
  4. See Health Checks for what each liveness probe checks