Construct a SQL agent from an LLM and toolkit or database.
This agent can execute arbitrary SQL against your database.
By default, the agent is allowed to generate SQL strings and run them via the database connection. This is powerful, but it also means the agent can generate expensive or dangerous queries (e.g., long-running queries, large scans/joins, locking queries, or unintended writes depending on your database permissions).
create_sql_agent returns a langchain_classic AgentExecutor.
AgentExecutor is an agent abstraction that has long been considered legacy
and is not actively supported as the recommended foundation for new production
applications.
For production-grade agent development, prefer building with Deep Agents: https://github.com/langchain-ai/deepagents
If you use this in production, coordinate with your security/DB teams and apply server-side controls:
Client-side timeouts do not always guarantee that a running statement is cancelled on the database server.
create_sql_agent(
llm: BaseLanguageModel,
toolkit: Optional[SQLDatabaseToolkit] = None,
agent_type: Optional[Union[AgentType, Literal['openai-tools', 'tool-calling']]] = None,
callback_manager: Optional[BaseCallbackManager] = None,
prefix: Optional[str] = None,
suffix: Optional[str] = None,
format_instructions: Optional[str] = None,
input_variables: Optional[List[str]] = None,
top_k: int = 10,
max_iterations: Optional[int] = 15,
max_execution_time: Optional[float] = None,
early_stopping_method: str = 'force',
verbose: bool = False,
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
extra_tools: Sequence[BaseTool] = (),
*,
db: Optional[SQLDatabase] = None,
prompt: Optional[BasePromptTemplate] = None,
**kwargs: Any = {}
) -> AgentExecutorExample:
.. code-block:: python
from langchain_openai import ChatOpenAI
from langchain_community.agent_toolkits import create_sql_agent
from langchain_community.utilities import SQLDatabase
db = SQLDatabase.from_uri("sqlite:///Chinook.db")
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
agent_executor = create_sql_agent(llm, db=db, agent_type="tool-calling", verbose=True)
| Name | Type | Description |
|---|---|---|
llm* | BaseLanguageModel | Language model to use for the agent. If agent_type is "tool-calling" then llm is expected to support tool calling. |
toolkit | Optional[SQLDatabaseToolkit] | Default: NoneSQLDatabaseToolkit for the agent to use. Must provide exactly one of 'toolkit' or 'db'. Specify 'toolkit' if you want to use a different model for the agent and the toolkit. |
agent_type | Optional[Union[AgentType, Literal['openai-tools', 'tool-calling']]] | Default: NoneOne of "tool-calling", "openai-tools", "openai-functions", or "zero-shot-react-description". Defaults to "zero-shot-react-description". "tool-calling" is recommended over the legacy "openai-tools" and "openai-functions" types. |
callback_manager | Optional[BaseCallbackManager] | Default: NoneDEPRECATED. Pass "callbacks" key into 'agent_executor_kwargs' instead to pass constructor callbacks to AgentExecutor. |
prefix | Optional[str] | Default: NonePrompt prefix string. Must contain variables "top_k" and "dialect". |
suffix | Optional[str] | Default: NonePrompt suffix string. Default depends on agent type. |
format_instructions | Optional[str] | Default: NoneFormatting instructions to pass to ZeroShotAgent.create_prompt() when 'agent_type' is "zero-shot-react-description". Otherwise ignored. |
input_variables | Optional[List[str]] | Default: NoneDEPRECATED. |
top_k | int | Default: 10Number of rows to query for by default. |
max_iterations | Optional[int] | Default: 15Passed to AgentExecutor init. |
max_execution_time | Optional[float] | Default: NonePassed to AgentExecutor init. |
early_stopping_method | str | Default: 'force'Passed to AgentExecutor init. |
verbose | bool | Default: FalseAgentExecutor verbosity. |
agent_executor_kwargs | Optional[Dict[str, Any]] | Default: NoneArbitrary additional AgentExecutor args. |
extra_tools | Sequence[BaseTool] | Default: ()Additional tools to give to agent on top of the ones that come with SQLDatabaseToolkit. |
db | Optional[SQLDatabase] | Default: NoneSQLDatabase from which to create a SQLDatabaseToolkit. Toolkit is created using 'db' and 'llm'. Must provide exactly one of 'db' or 'toolkit'. |
prompt | Optional[BasePromptTemplate] | Default: NoneComplete agent prompt. prompt and {prefix, suffix, format_instructions, input_variables} are mutually exclusive. |
**kwargs | Any | Default: {}Arbitrary additional Agent args. |