provideAgent

Wire the LangGraph adapter into Angular's dependency injection.

Registers a singleton LangGraphAgent constructed from config. Retrieve it in any component with injectAgent(). Provide this at the application root (app.config.ts) for an app-wide agent.

To use a different agent in a component subtree, re-provide provideAgent({...}) in that component's providers: [] array — Angular's hierarchical DI scopes the singleton accordingly.

Static vs factory config. Pass a plain AgentConfig object when the config is known up front. Pass a () => AgentConfig factory when the config depends on runtime/DI state — the factory runs inside an Angular injection context, so it may call inject() to read services, route params, or component-scoped signals.

Typed state via AgentRef. Pass a typed ref as the first argument to flow the state shape from provideAgent to injectAgent without repeating the generic at every call site.

Several agents at one injector level. Each provideAgent(ref, …) call builds its own agent from its own config, so two (or more) refs may be provided side by side in a single providers array and injectAgent(refA) / injectAgent(refB) return distinct agents. The ref-less injectAgent() resolves a single shared token, which can only point at one of them: when more than one ref is provided at the same level the last call wins, and resolving it in dev mode logs a console.warn naming every competing ref. The AGENT_LIFECYCLE token follows the same rule. Always inject by ref when an injector provides more than one agent.

Lifecycle token. Every form also provides AGENT_LIFECYCLE, so inject(AGENT_LIFECYCLE) returns the same object as injectAgent().lifecycle without reaching for the agent itself.

import { provideAgent } from '@threadplane/langgraph';

Declaration · overload 1

export declare function provideAgent<T = Record<string, unknown>>(ref: AgentRef<T>, configOrFactory: AgentConfig<T> | (() => AgentConfig<T>)): Provider[];

Type parameters

T
Default: Record<string, unknown>

Parameters

ref · required
AgentRef<T>
configOrFactory · required
AgentConfig<T> | (() => AgentConfig<T>)

Returns

Provider[]

Examples

Two agents in one providers array

export const LIVE = createAgentRef<ChatState>('live');
export const REPLAY = createAgentRef<ChatState>('replay');
providers: [
  provideAgent(LIVE, { assistantId: 'chat' }),
  provideAgent(REPLAY, { assistantId: 'chat', transport: replayTransport }),
];
// component: injectAgent(LIVE) !== injectAgent(REPLAY)

Factory config reading route params

providers: [
  provideAgent(() => {
    const route = inject(ActivatedRoute);
    return { assistantId: 'chat', threadId: toSignal(route.paramMap) };
  }),
];

Typed state via AgentRef

export const TRIP = createAgentRef<TripState>('trip');
// app.config.ts:
providers: [provideAgent(TRIP, { assistantId: 'trip-graph' })]
// component:
const agent = injectAgent(TRIP); // LangGraphAgent<TripState>

Declaration · overload 2

export declare function provideAgent<T = Record<string, unknown>>(configOrFactory: AgentConfig<T> | (() => AgentConfig<T>)): Provider[];

Type parameters

T
Default: Record<string, unknown>

Parameters

configOrFactory · required
AgentConfig<T> | (() => AgentConfig<T>)

Returns

Provider[]