Agent applications are often viewed as unduly expensive to develop and maintain in commercial contexts. Organizations often settle for less sophisticated and more traditional software in place of agent technology because of (often misplaced) fears about the development and maintenance costs of agent technology, and the often mistaken perception that traditional software offers better returns on investment. This paper aims to redress this by developing a plan recognition framework for agent program learning, where behavior logs of legacy applications (or even manually executed processes) are mined to extract a ‘draft’ version of agent code that could eventually replace these applications or processes. We develop, implement and evaluate techniques for inferring agent plans from behavior logs, with both positive and negative examples. After obtaining the plans, we resort to an effect log to identify the context (i.e. precondition) for each plan. The experimental results show that our framework generates a first draft of an agent program (i.e. the code) which can then be modified as required by a developer.