> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cedarcopilot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Advanced Context Management

> Handle complex state serialization and circular dependencies

## Handling Circular Dependencies with sanitiseJson

Cedar automatically applies `sanitiseJson` to your state when sending it as agent context, which intelligently handles circular dependencies by replacing them with reference paths (e.g., `"$ref:$.parent.child"`). If your backend has access to Cedar utilities, you can use `desanitiseJson` to restore the original object structure with all circular references intact.

For cases where you need precise control over the JSON structure being sent, pre-stringify your state before adding it to the context. This bypasses automatic sanitization, ensuring the exact shape you define is transmitted to your agent backend. This approach is particularly useful when working with backends that don't support reference resolution or when you need to maintain specific data formats.

```tsx theme={null}
useSubscribeStateToAgentContext<typeof Data>(
	'dashboard-data',
	(state) => {
		console.log('dashboard-data', state);
		return {
			Data: Flatten.stringify(state),
		};
	},
	{
		labelField: 'title',
	}
);
```
