{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/automatic-prompt-optimization-with-gradient","title":"Automatic Prompt Optimization with \"Gradient Descent\" and Beam Search","arxiv_id":"2305.03495","date":"2023-05-04","proceeding":null,"authors":["Reid Pryzant","Dan Iter","Jerry Li","Yin Tat Lee","Chenguang Zhu","Michael Zeng"],"abstract":"Large Language Models (LLMs) have shown impressive performance as general purpose agents, but their abilities remain highly dependent on prompts which are hand written with onerous trial-and-error effort. We propose a simple and nonparametric solution to this problem, Automatic Prompt Optimization (APO), which is inspired by numerical gradient descent to automatically improve prompts, assuming access to training data and an LLM API. The algorithm uses minibatches of data to form natural language \"gradients\" that criticize the current prompt. The gradients are then \"propagated\" into the prompt by editing the prompt in the opposite semantic direction of the gradient. These gradient descent steps are guided by a beam search and bandit selection procedure which significantly improves algorithmic efficiency. Preliminary results across three benchmark NLP tasks and the novel problem of LLM jailbreak detection suggest that Automatic Prompt Optimization can outperform prior prompt editing techniques and improve an initial prompt's performance by up to 31%, by using data to rewrite vague task descriptions into more precise annotation instructions.","url_abs":"https://arxiv.org/abs/2305.03495v2","url_pdf":"https://arxiv.org/pdf/2305.03495v2.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"automatic-prompt-optimization-with-gradient","repo_url":"https://github.com/microsoft/lmops","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"jax","reach":null},{"paper_slug":"automatic-prompt-optimization-with-gradient","repo_url":"https://github.com/batorskq/prl","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok"}},{"paper_slug":"automatic-prompt-optimization-with-gradient","repo_url":"https://github.com/seasonyao/automatic_prompt_optimization_physician_prompting","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"none","reach":{"status":"ok","spdx":"Apache-2.0"}},{"paper_slug":"automatic-prompt-optimization-with-gradient","repo_url":"https://github.com/yongchao98/promst","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}},{"paper_slug":"automatic-prompt-optimization-with-gradient","repo_url":"https://github.com/zcrwind/prefer","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"automatic-prompt-optimization-with-gradient","repo_url":"https://github.com/lastmile-ai/aiconfig","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":0,"framework":"none","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"llm-jailbreak","task_name":"LLM Jailbreak"}],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2305.03495","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2305.03495"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/batorskq/prl","reach":{"status":"ok"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/seasonyao/automatic_prompt_optimization_physician_prompting","reach":{"status":"ok","spdx":"Apache-2.0"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/yongchao98/promst","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/lastmile-ai/aiconfig","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/zcrwind/prefer","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/microsoft/lmops","reach":null}],"summary":{"ran":4,"ran_draft_wrong":1,"unverified":4},"by_repo_kind":{"official":{"samples":4,"ran":2,"repositories":1},"listed":{"samples":5,"ran":3,"repositories":2}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"e980abcb95fe131a","entry":"BaseModel","repo":"zcrwind/prefer","repo_kind":"listed","path":"src/ptuning.py","file_url":"https://github.com/zcrwind/prefer/blob/HEAD/src/ptuning.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e980abcb95fe131a"}},{"code_sha256_prefix":"9568c0c898993622","entry":"ProTeGi","repo":"microsoft/lmops","repo_kind":"official","path":"prompt_optimization/optimizers.py","file_url":"https://github.com/microsoft/lmops/blob/HEAD/prompt_optimization/optimizers.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"9568c0c898993622"}},{"code_sha256_prefix":"ae0cdcbf80c23151","entry":"make_triples","repo":"seasonyao/automatic_prompt_optimization_physician_prompting","repo_kind":"listed","path":"src/APO_Medical_Prompting/metrics.py","file_url":"https://github.com/seasonyao/automatic_prompt_optimization_physician_prompting/blob/HEAD/src/APO_Medical_Prompting/metrics.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"ae0cdcbf80c23151"}},{"code_sha256_prefix":"37e7baf06cc5b2f0","entry":"parse_sectioned_prompt","repo":"microsoft/lmops","repo_kind":"official","path":"prompt_optimization/optimizers.py","file_url":"https://github.com/microsoft/lmops/blob/HEAD/prompt_optimization/optimizers.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"37e7baf06cc5b2f0"}},{"code_sha256_prefix":"814172c8e37b1334","entry":"process_triples","repo":"seasonyao/automatic_prompt_optimization_physician_prompting","repo_kind":"listed","path":"src/APO_Medical_Prompting/metrics.py","file_url":"https://github.com/seasonyao/automatic_prompt_optimization_physician_prompting/blob/HEAD/src/APO_Medical_Prompting/metrics.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"814172c8e37b1334"}},{"code_sha256_prefix":"f3b032f2bc5e6cc4","entry":"PromptOptimizer","repo":"microsoft/lmops","repo_kind":"official","path":"prompt_optimization/optimizers.py","file_url":"https://github.com/microsoft/lmops/blob/HEAD/prompt_optimization/optimizers.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"f3b032f2bc5e6cc4"}},{"code_sha256_prefix":"9295971639a2393d","entry":"RoBERTaVTuningClassification","repo":"zcrwind/prefer","repo_kind":"listed","path":"src/ptuning.py","file_url":"https://github.com/zcrwind/prefer/blob/HEAD/src/ptuning.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"9295971639a2393d"}},{"code_sha256_prefix":"4fa6d30b6afe3ef8","entry":"chatgpt","repo":"microsoft/lmops","repo_kind":"official","path":"prompt_optimization/optimizers.py","file_url":"https://github.com/microsoft/lmops/blob/HEAD/prompt_optimization/optimizers.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"4fa6d30b6afe3ef8"}},{"code_sha256_prefix":"a82a905574e3548e","entry":"remove_stopword_and_punc_in_list","repo":"seasonyao/automatic_prompt_optimization_physician_prompting","repo_kind":"listed","path":"src/APO_Medical_Prompting/metrics.py","file_url":"https://github.com/seasonyao/automatic_prompt_optimization_physician_prompting/blob/HEAD/src/APO_Medical_Prompting/metrics.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"a82a905574e3548e"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}