New Relic Python Agent
Official APM agent for Python applications. Auto-instruments web frameworks (FastAPI, Django, Flask), databases (SQLAlchemy, psycopg, asyncpg), Redis, external HTTP calls, and AI/LLM libraries. Current version is 11.4.0 (Jan 2026). v12.0.0 released March 2026 with breaking removals.
Common errors
-
newrelic-admin: not found
cause The `newrelic-admin` wrapper script is not found in the system's PATH, or the New Relic Python agent is not installed in the currently active Python environment.fixEnsure the New Relic Python agent is installed in your application's virtual environment or system-wide, and that the `newrelic-admin` script (usually in `bin/` or `Scripts/` within your Python environment) is accessible via your system's PATH. You might need to activate your virtual environment or use its direct path. -
NEWRELIC: ... - New Relic could not start because the newrelic-admin script was called from a Python installation that is different from the Python installation that is currently running.
cause This error occurs when the Python interpreter running the application differs from the Python interpreter used to run the `newrelic-admin` script, often seen in environments with multiple Python versions or virtual environments.fixEnsure that the `newrelic-admin` script and your application are executed using the *same* Python installation and virtual environment. This might involve explicitly calling the `newrelic-admin` script from the correct virtual environment's `bin` directory, e.g., `/path/to/venv/bin/newrelic-admin run-program your_app.py`. -
Initialization failed: Configuration has already been done against differing configuration file or environment.
cause The New Relic Python agent's `newrelic.agent.initialize()` function was called multiple times with conflicting configuration parameters (different config files or environments), or conflicting environment variables/server-side configurations are present.fixInitialize the agent only once at the very beginning of your application's startup. Ensure consistency in configuration by using a single method (config file, environment variables, or `newrelic.agent.initialize()`) and avoiding conflicting settings across these methods. Environment variables override agent defaults, the config file overrides environment variables, and server-side config overrides the config file. -
ERROR - No license key was set in agent configuration.
cause The New Relic Python agent cannot connect to the collector because the `license_key` is missing from the `newrelic.ini` configuration file or the `NEW_RELIC_LICENSE_KEY` environment variable is not set.fixProvide your New Relic license key either in the `license_key` setting within your `newrelic.ini` file or by setting the `NEW_RELIC_LICENSE_KEY` environment variable before your application starts. -
Python agent not reporting handled exceptions
cause By default, the New Relic Python agent only reports *unhandled* exceptions that cause a transaction to end. Explicitly caught exceptions are not reported unless specifically instructed.fixTo report handled exceptions, use `newrelic.agent.notice_error()` or `newrelic.agent.record_exception()` within your `except` blocks. These functions allow you to explicitly send exception details to New Relic APM.
Warnings
- breaking initialize() must be called before ALL other imports. Placing it after framework or library imports silently disables instrumentation — no exception is raised, agent appears to load but instruments nothing.
- breaking Cross Application Tracing (CAT) fully removed in v12.0.0. Was deprecated since v7.0.0. Old tutorials and LLM-generated code using cross_application_tracer config keys will silently do nothing.
- breaking Python 3.8 support dropped in v12.0.0 (Python 3.8 EOL was Oct 2024).
- deprecated aioredis instrumentation deprecated. The aioredis package itself is abandoned. New Relic will remove this instrumentation in a future release.
- deprecated Configuring agent via WSGI environ dictionary is deprecated.
- gotcha SSL certificates no longer bundled in v12.0.0. Environments without system CA certs (minimal Docker images) will fail to connect to New Relic.
- breaking New Relic agent initialization failed because the specified configuration file (e.g., 'newrelic.ini') could not be found or was inaccessible.
- breaking Calling `newrelic.agent.initialize(config_file)` requires the specified configuration file to exist and be accessible at the provided path. If the file is not found or cannot be read, a `newrelic.api.exceptions.ConfigurationError` will be raised.
Install
-
pip install newrelic -
newrelic-admin generate-config YOUR_LICENSE_KEY newrelic.ini -
pip install newrelic[certificates]
Imports
- agent initialize
from fastapi import FastAPI import newrelic.agent newrelic.agent.initialize('newrelic.ini') # too late — FastAPI already importedimport newrelic.agent newrelic.agent.initialize('newrelic.ini') # MUST be first lines before any framework imports - CLI runner (recommended)
gunicorn myapp.wsgi # agent never loaded
NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-program gunicorn myapp.wsgi
Quickstart
# Option 1: CLI wrapper (recommended — handles ordering automatically)
# NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-program gunicorn myapp.wsgi
# Option 2: Manual init — must be absolute first lines of entry point
import newrelic.agent
newrelic.agent.initialize('newrelic.ini')
# Framework imports AFTER initialize()
from fastapi import FastAPI
app = FastAPI()