Getting started
editInstall the package
Add the Meziantou.Framework.LLMContext package to a project:
dotnet package add Meziantou.Framework.LLMContextThe examples of this documentation omit the using directives: they need using Meziantou.Framework.LLMContext;.
Detect an LLM context
LLMContextDetector.IsLLMContext() returns true when the environment variables of the process match at least one known tool, and LLMContextDetector.Detect() returns the tools it matches:
if (LLMContextDetector.IsLLMContext())
{
IReadOnlyList<LLMContextKind> contexts = LLMContextDetector.Detect();
Assert.NotEmpty(contexts);
}List the detected tools
Detect returns an IReadOnlyList<LLMContextKind>: one value per tool whose variables are set, or an empty list. Several tools can be detected at once, for instance when an agent runs inside an editor that is detected too:
// Several contexts can be detected at once, for instance an agent started from another one
foreach (LLMContextKind context in LLMContextDetector.Detect())
{
string name = context switch
{
LLMContextKind.ClaudeCode => "Claude Code",
LLMContextKind.GitHubCopilot => "GitHub Copilot",
LLMContextKind.GenericAgent => "an agent",
_ => context.ToString(),
};
Assert.NotEmpty(name);
}
// IsLLMContext is true when Detect finds at least one context
Assert.Equal(LLMContextDetector.Detect().Count > 0, LLMContextDetector.IsLLMContext());- The values are always returned in the same order, the order of the LLMContextKind enumeration, whatever the order of the variables.
- LLMContextKind.GenericAgent is not a specific tool: it is the
AGENT_CLIvariable, that any agent can set. See Detection rules. - Each call reads the environment variables again: a variable set or removed with
Environment.SetEnvironmentVariablechanges the result of the next call.
Next steps
- Detection rules: the variables each tool is recognized by.