---
title: Event context
description: Learn how to add custom metadata to events sent to Honeybadger Insights.
url: https://docs.honeybadger.io/lib/python/insights/event-context/
---

You can add custom metadata to the events sent to Honeybadger Insights by using the `honeybadger.set_event_context` function. This metadata will be included in each event call within the current thread.

Caution

This will add the metadata to all events sent, so be careful not to include too much data. Try to keep it to simple key/value pairs.

```python
from honeybadger import honeybadger


honeybadger.set_event_context(
    user_id=user.id,
    session_id=session.id
)
```

## Context lifecycle

Event context is automatically cleared at the beginning of each web request in supported frameworks ([Django](https://docs.honeybadger.io/lib/python/integrations/django/), [Flask](https://docs.honeybadger.io/lib/python/integrations/flask/)). You can also manually clear it:

```python
honeybadger.reset_event_context()
```

## Temporary context

For temporary context that should only apply to specific operations, use the context manager:

```python
with honeybadger.event_context(operation="batch_import"):
    # Context is only set within this block
    honeybadger.event("import.started")
    process_batch()
    honeybadger.event("import.completed")
# Context is automatically restored when exiting the block
```

## Precedence

If both event context and event data contain the same key, the event data takes precedence:

```python
honeybadger.set_event_context(service="api")
honeybadger.event("order.created", service="checkout")  # service="checkout" is used
```

---

## Try Honeybadger for FREE

Intelligent logging, error tracking, and Just Enough APM™ in one dev-friendly platform. Find and fix problems before users notice.

[Start free trial](https://app.honeybadger.io/users/sign_up)

[See plans and pricing](https://www.honeybadger.io/plans/)
